Spaces:
Running
Running
Download python/borderland_web/server.py from FatUnc6969/borderland-rl: direct link, hf CLI and curl.
- Browser
- Download file 24.1 kB
-
https://huggingface.co/spaces/FatUnc6969/borderland-rl/resolve/main/python/borderland_web/server.py
- Command line
-
hf download hf://spaces/FatUnc6969/borderland-rl/python/borderland_web/server.py
-
curl -L -o server.py https://huggingface.co/spaces/FatUnc6969/borderland-rl/resolve/main/python/borderland_web/server.py
24.1 kB
| from __future__ import annotations | |
| from collections import OrderedDict | |
| from dataclasses import dataclass, field | |
| from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer | |
| from ipaddress import IPv4Address | |
| import json | |
| import os | |
| from pathlib import Path | |
| import re | |
| import secrets | |
| import sys | |
| import threading | |
| import time | |
| MAX_BODY = 32768 | |
| STATIC = Path(__file__).parent / 'static' | |
| SESSION_ID = re.compile(r'^[a-f0-9]{32}$') | |
| HTTPS_ORIGIN = re.compile(r'https://([a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*)(?::([1-9][0-9]{0,4}))?') | |
| def _https_origin(value): | |
| match = HTTPS_ORIGIN.fullmatch(value) if isinstance(value, str) else None | |
| if not match or len(match[1]) > 253 or (match[2] and int(match[2]) > 65535): | |
| raise ValueError('Deployment origins must be exact lowercase HTTPS origins without paths, credentials, or wildcards.') | |
| if match[1].rsplit('.', 1)[-1].isdigit(): | |
| IPv4Address(match[1]) | |
| return value | |
| class DeploymentConfig: | |
| public_origin: str | None = None | |
| frame_ancestors: tuple[str, ...] = () | |
| def __post_init__(self): | |
| if self.public_origin is not None: | |
| _https_origin(self.public_origin) | |
| if not isinstance(self.frame_ancestors, tuple) or len(self.frame_ancestors) > 8: | |
| raise ValueError('Frame ancestors must be a tuple of at most eight exact HTTPS origins.') | |
| for ancestor in self.frame_ancestors: | |
| _https_origin(ancestor) | |
| if len(set(self.frame_ancestors)) != len(self.frame_ancestors): | |
| raise ValueError('Frame ancestors must be unique.') | |
| def from_env(cls): | |
| ancestors = os.environ.get('BORDERLAND_FRAME_ANCESTORS') | |
| return cls(os.environ.get('BORDERLAND_PUBLIC_ORIGIN'), tuple(ancestors.split(',')) if ancestors is not None else ()) | |
| def csp(self): | |
| ancestors = ' '.join(self.frame_ancestors) or "'none'" | |
| return "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; frame-ancestors " + ancestors + "; base-uri 'none'; form-action 'self'" | |
| class RequestError(Exception): | |
| def __init__(self, status, message): | |
| self.status, self.message = status, message | |
| class Session: | |
| campaign: object | |
| transition: dict | |
| touched: float = field(default_factory=time.monotonic) | |
| receipts: OrderedDict = field(default_factory=OrderedDict) | |
| lock: threading.Lock = field(default_factory=threading.Lock) | |
| class Application: | |
| def __init__(self, campaign_factory=None, archive_workload="standard", generation_version=3, notebook_bytes=768, memory_profile="static"): | |
| if archive_workload not in ("compact","standard","extended"): | |
| raise ValueError("Invalid Archive workload.") | |
| if type(generation_version) is not int or generation_version not in (2, 3): | |
| raise ValueError("Generation version must be 2 or 3.") | |
| if type(notebook_bytes) is not int or not 0 <= notebook_bytes <= 8192: | |
| raise ValueError("Notebook capacity must be an integer from 0 to 8192 bytes.") | |
| if memory_profile not in ("static", "commissioning") or (generation_version == 2 and memory_profile != "static"): | |
| raise ValueError("Commissioning requires generation three.") | |
| self.archive_workload = archive_workload | |
| self.generation_version = generation_version | |
| self.notebook_bytes = notebook_bytes | |
| self.memory_profile = memory_profile | |
| if campaign_factory is None: | |
| from borderland import Campaign | |
| campaign_factory = lambda: Campaign.generated(secrets.randbits(63), archive_workload=archive_workload, generation_version=generation_version, notebook_bytes=notebook_bytes, memory_profile=memory_profile) | |
| self.campaign_factory = campaign_factory | |
| self.sessions = {} | |
| self.lock = threading.Lock() | |
| def create(self): | |
| with self.lock: | |
| now = time.monotonic() | |
| self.sessions = {k: v for k, v in self.sessions.items() if now - v.touched < 7200} | |
| if len(self.sessions) >= 256: | |
| raise RequestError(503, 'The session limit is reached. Try again later.') | |
| campaign = self.campaign_factory() | |
| transition = campaign.reset().to_dict() | |
| session_id = secrets.token_hex(16) | |
| self.sessions[session_id] = Session(campaign, transition) | |
| return {'session': session_id, 'transition': transition} | |
| def session(self, session_id): | |
| if not isinstance(session_id, str) or not SESSION_ID.fullmatch(session_id): | |
| raise RequestError(404, 'Session not found.') | |
| with self.lock: | |
| session = self.sessions.get(session_id) | |
| if session is None or time.monotonic() - session.touched >= 7200: | |
| raise RequestError(404, 'Session expired. Start a new campaign.') | |
| session.touched = time.monotonic() | |
| return session | |
| def command(self, route, body): | |
| required = {'session', 'revision', 'request_id', 'action' if route == 'action' else 'text'} | |
| if set(body) != required: | |
| raise RequestError(400, 'Command fields do not match the request schema.') | |
| if type(body['revision']) is not int or body['revision'] < 0: | |
| raise RequestError(400, 'Revision must be a nonnegative integer.') | |
| request_id = body['request_id'] | |
| if not isinstance(request_id, str) or not re.fullmatch(r'[A-Za-z0-9_-]{8,80}', request_id): | |
| raise RequestError(400, 'A request ID of 8 to 80 letters, digits, underscores or hyphens is required.') | |
| if route == 'notebook' and (not isinstance(body['text'], str) or len(body['text']) > 8192): | |
| raise RequestError(400, 'Notebook text is invalid or too long.') | |
| if route == 'action': | |
| action = body['action'] | |
| if not isinstance(action, dict) or set(action) != {'tag', 'arguments'}: | |
| raise RequestError(400, 'Action requires exactly tag and arguments.') | |
| if not isinstance(action['tag'], str) or not isinstance(action['arguments'], dict): | |
| raise RequestError(400, 'Action tag and arguments have invalid types.') | |
| session = self.session(body['session']) | |
| fingerprint = json.dumps([route, body], sort_keys=True, separators=(',', ':'), allow_nan=False) | |
| with session.lock: | |
| if request_id in session.receipts: | |
| old_fingerprint, status, receipt = session.receipts[request_id] | |
| if old_fingerprint != fingerprint: | |
| raise RequestError(409, 'This request ID was already used for a different command.') | |
| return status, receipt | |
| if len(session.receipts) >= 1024: | |
| raise RequestError(409, 'This session has reached its command limit. Start a new campaign.') | |
| if body['revision'] != session.transition['revision']: | |
| receipt = {'session': body['session'], 'request_id': request_id, 'error': 'State changed. Review the current observation before acting.', 'transition': session.transition} | |
| session.receipts[request_id] = fingerprint, 409, receipt | |
| return 409, receipt | |
| if route == 'action': | |
| transition = session.campaign.step(body['action'], expected_revision=body['revision']) | |
| else: | |
| transition = session.campaign.replace_notebook(body['text'], expected_revision=body['revision']) | |
| session.transition = transition.to_dict() | |
| status = 422 if session.transition.get('error') else 200 | |
| receipt = {'session': body['session'], 'request_id': request_id, 'transition': session.transition} | |
| session.receipts[request_id] = fingerprint, status, receipt | |
| return status, receipt | |
| def _reference_replay(self, campaign, stop_before=None): | |
| from borderland_verify.reference import public_reference | |
| transition = campaign.reset() | |
| policy = public_reference(transition) | |
| frames = [{'transition': transition.to_dict(), 'action': None, 'kind': 'start'}] | |
| resets = 0 | |
| for _ in range(320): | |
| if transition.done: | |
| break | |
| note = policy.notebook(transition) | |
| if note is not None and note != transition.observation.notebook['text']: | |
| transition = campaign.replace_notebook(note, expected_revision=transition.revision) | |
| frames.append({'transition': transition.to_dict(), 'action': None, 'kind': 'notebook'}) | |
| action = policy.choose(transition) | |
| if stop_before is not None and stop_before(action, transition): | |
| break | |
| transition = campaign.step(action, expected_revision=transition.revision) | |
| frames.append({'transition': transition.to_dict(), 'action': action.to_dict(), 'kind': 'action'}) | |
| if transition.context_reset: | |
| policy = public_reference(transition) | |
| resets += 1 | |
| if transition.error: | |
| break | |
| return {'label': 'Scripted public reference', 'generation_version': self.generation_version, | |
| 'scope': 'Actual public engine events from a separate campaign. A fresh scripted controller receives only the saved notebook and current state at Blackout. No Beam calls.', | |
| 'frames': frames, 'outcome': transition.outcome, 'controller_resets': resets, 'memory_profile': self.memory_profile} | |
| def demonstration(self): | |
| from borderland import Campaign | |
| return self._reference_replay(Campaign.generated(17, archive_workload=self.archive_workload, | |
| generation_version=self.generation_version, notebook_bytes=self.notebook_bytes, memory_profile=self.memory_profile)) | |
| def twins(self): | |
| if self.generation_version == 2: | |
| from borderland_verify.certificates import certify_twins | |
| return {'label': 'Legacy generation 2 assisted final-valve checkpoint', | |
| 'generation_version': 2, 'certificate': certify_twins(17)} | |
| from borderland import Action, Campaign | |
| from borderland.worlds import candidate, coherent_twin | |
| from borderland.plant import circuit_bus, circuit_order, exit_decision, compensated_bus | |
| base = candidate(0, generation_version=3, archive_workload=self.archive_workload, | |
| notebook_bytes=self.notebook_bytes, memory_profile=self.memory_profile) | |
| channel = base.infrastructure.calibrations[0] | |
| twin = coherent_twin(base, sensor_xor=1-channel.sensor_xor) | |
| worlds = (base, twin) | |
| replays = [self._reference_replay(Campaign(world)) for world in worlds] | |
| hub_prefix = [] | |
| for frame in replays[0]['frames'][1:]: | |
| if frame['action'] is not None and frame['transition']['observation']['phase'] == 4: | |
| if frame['action']['tag'] == 'bank': | |
| break | |
| if frame['action']['tag'] != 'bind_docket': | |
| hub_prefix.append(frame['action']) | |
| def selected_bus(world, service, index): | |
| plant = world.infrastructure | |
| bus = circuit_bus(plant, service) | |
| if plant.load_schedule is not None: | |
| meter = plant.load_schedule.test(service, index) | |
| bus = compensated_bus(bus, plant.load_schedule.curve(plant.current(service).id), meter.band, meter.load) | |
| return bus | |
| target, test_index = next((cid, index) for cid in circuit_order(base.infrastructure) | |
| for index in range(3 if self.memory_profile == 'commissioning' else 1) | |
| if selected_bus(base, cid, index) != selected_bus(twin, cid, index)) | |
| commands = [ | |
| ('delivery', 'The same flag bank pays different customs charge.', | |
| lambda action, transition: action.tag == 'bank', [Action('bank', {'actor': 'Ada'})]), | |
| ('circuit', 'The same bus and energize command passes a commissioning test versus tripping a circuit.' if self.memory_profile == 'commissioning' else 'The same bus and energize command produces live versus tripped circuits.', | |
| lambda action, transition: action.tag == 'connect' and action.arguments['circuit'] == target | |
| and next(c for c in transition.observation.board['circuits'] if c['id'] == target).get('completed_tests', 0) == test_index, | |
| [Action('connect', {'circuit': target, 'bus': selected_bus(base, target, test_index)}), | |
| Action('energize', {'circuit': target})]), | |
| ('exit', 'The same valve and launch commands preserve versus vent pressure.', | |
| lambda action, transition: action.tag == 'open_valve', | |
| [Action('open_valve', {'side': exit_decision(base.infrastructure)[1]}), Action('launch', {})]), | |
| ] | |
| comparisons = [] | |
| for kind, description, stop, actions in commands: | |
| branches = [] | |
| for world in worlds: | |
| campaign = Campaign(world) | |
| checkpoint = (lambda action, transition: transition.observation.phase == 4) if kind == 'delivery' else stop | |
| replay = self._reference_replay(campaign, stop_before=checkpoint) | |
| if kind == 'delivery': | |
| for command in hub_prefix: | |
| staged = campaign.step(command) | |
| if staged.error: | |
| raise RuntimeError('The shared delivery probe prefix was rejected.') | |
| replay['frames'].append({'transition': staged.to_dict(), 'action': command, 'kind': 'action'}) | |
| before = replay['frames'][-1]['transition'] | |
| events = [] | |
| revision = before['revision'] | |
| for action in actions: | |
| transition = campaign.step(action, expected_revision=revision) | |
| revision = transition.revision | |
| events.append({'transition': transition.to_dict(), 'action': action.to_dict(), 'kind': 'action'}) | |
| branches.append({'before': before, 'events': events, 'prefix': replay, | |
| 'charge_spent': before['observation']['charge']-events[-1]['transition']['observation']['charge']}) | |
| comparisons.append({'kind': kind, 'description': description, 'branches': branches, | |
| **({'service': target, 'test': test_index + 1} if kind == 'circuit' else {})}) | |
| result = {'label': 'Generation 3 coherent causal replays', 'generation_version': 3, | |
| 'scope': 'One channel wiring bit changes. Both complete replays use the public scripted controller. Separate checkpoint probes apply the same commands in both worlds. These examples establish no unrestricted memory bound or Beam result.', | |
| 'intervention': {'channel': channel.id, 'field': 'sensor wiring bit', | |
| 'observed_at_latch_zero': [channel.sensor_xor, 1-channel.sensor_xor]}, | |
| 'replays': replays, 'comparisons': comparisons} | |
| if self.memory_profile == 'commissioning': | |
| result['information_check'] = self._curve_information_check() | |
| return result | |
| def _curve_information_check(): | |
| from borderland_verify.curve_twins import certify_curve_twins | |
| checked = certify_curve_twins() | |
| summary = checked['summary'] | |
| result = {'label': 'Separate known standard seed 16 check', | |
| 'archive_workload': 'standard', 'notebook_bytes': 768, | |
| 'memory_profile': 'commissioning', 'summary': summary} | |
| if not summary['passed']: | |
| return result | |
| evidence_decision = summary['archive_evidence'][0]['decision'] | |
| branches = [] | |
| for label in ('low', 'high'): | |
| trace = checked['public_command_traces'][label] | |
| earlier = next(row for row in trace if row['decision'] == evidence_decision) | |
| target = next(row for row in trace if row['request']['tool'] == 'execute_action' | |
| and row['request']['arguments']['tag'] == 'connect' | |
| and row['request']['arguments']['arguments']['circuit'] == summary['target']['service']) | |
| branches.append({'label': label, | |
| 'earlier_evidence': {'decision': earlier['decision'], 'public': earlier['public']}, | |
| 'saved_notebook': target['public']['observation']['notebook'], | |
| 'first_target_connect': target}) | |
| result.update(branches=branches, erased_public_checkpoint=checked['erased_public_checkpoint']) | |
| return result | |
| def _object(pairs): | |
| result = {} | |
| for key, value in pairs: | |
| if key in result: | |
| raise ValueError('Duplicate JSON keys are not accepted.') | |
| result[key] = value | |
| return result | |
| def make_server(host='127.0.0.1', port=8765, application=None, deployment=None): | |
| application = application or Application() | |
| deployment = DeploymentConfig.from_env() if deployment is None else deployment | |
| if not isinstance(deployment, DeploymentConfig): | |
| raise TypeError('Deployment configuration must be a DeploymentConfig.') | |
| class Handler(BaseHTTPRequestHandler): | |
| def log_message(self, format, *args): | |
| pass | |
| def send(self, status, payload, content_type='application/json; charset=utf-8'): | |
| data = json.dumps(payload, ensure_ascii=False, allow_nan=False).encode() if content_type.startswith('application/json') else payload | |
| self.send_response(status) | |
| self.send_header('Content-Type', content_type) | |
| self.send_header('Content-Length', str(len(data))) | |
| self.send_header('Cache-Control', 'no-store') | |
| self.send_header('X-Content-Type-Options', 'nosniff') | |
| self.send_header('Referrer-Policy', 'no-referrer') | |
| self.send_header('Content-Security-Policy', deployment.csp) | |
| self.end_headers() | |
| self.wfile.write(data) | |
| def do_GET(self): | |
| try: | |
| if self.path == '/api/recordings': | |
| self.send(200, json.loads((STATIC / 'beam-recordings.json').read_text(encoding='utf-8'))) | |
| return | |
| if self.path.startswith('/api/session/'): | |
| session = application.session(self.path.removeprefix('/api/session/')) | |
| with session.lock: | |
| self.send(200, {'transition': session.transition}) | |
| return | |
| files = {'/': ('index.html', 'text/html; charset=utf-8'), '/app.js': ('app.js', 'text/javascript; charset=utf-8'), '/style.css': ('style.css', 'text/css; charset=utf-8'), | |
| '/proof.html': ('proof.html', 'text/html; charset=utf-8'), '/proof.js': ('proof.js', 'text/javascript; charset=utf-8'), | |
| '/proof.css': ('proof.css', 'text/css; charset=utf-8'), '/proof.json': ('proof.json', 'application/json'), | |
| '/probe.html': ('probe.html', 'text/html; charset=utf-8'), '/probe.js': ('probe.js', 'text/javascript; charset=utf-8'), | |
| '/probe.css': ('probe.css', 'text/css; charset=utf-8'), '/probe.json': ('probe.json', 'application/json'), | |
| '/campaign-pilot.html': ('campaign-pilot.html', 'text/html; charset=utf-8'), | |
| '/campaign-pilot.js': ('campaign-pilot.js', 'text/javascript; charset=utf-8'), | |
| '/campaign-pilot.json': ('campaign-pilot.json', 'application/json'), | |
| '/campaign-pilot-receipts.zip': (STATIC.parent.parent / 'evidence/archive-campaign-pilot-v1/public-receipts.zip', 'application/zip'), | |
| '/probe-receipts.zip': (STATIC.parent.parent / 'evidence/archive-probe-v1/public-receipts.zip', 'application/zip')} | |
| if self.path not in files: | |
| raise RequestError(404, 'Route not found.') | |
| filename, content_type = files[self.path] | |
| payload = (STATIC / filename).read_bytes() | |
| self.send(200, json.loads(payload) if content_type.startswith('application/json') else payload, content_type) | |
| except RequestError as exc: | |
| self.send(exc.status, {'error': exc.message}) | |
| def do_POST(self): | |
| try: | |
| if self.path not in {'/api/session', '/api/action', '/api/notebook', '/api/demo', '/api/twins'}: | |
| raise RequestError(404, 'Route not found.') | |
| origins = self.headers.get_all('Origin', []) | |
| if origins and (len(origins) != 1 or origins[0] not in allowed_origins): | |
| raise RequestError(403, 'Cross-origin commands are not accepted.') | |
| if self.headers.get('Transfer-Encoding') or len(self.headers.get_all('Content-Length', [])) != 1: | |
| raise RequestError(400, 'One Content-Length header is required.') | |
| try: | |
| length = int(self.headers['Content-Length']) | |
| except (TypeError, ValueError): | |
| raise RequestError(400, 'Invalid content length.') | |
| if length < 0 or length > MAX_BODY: | |
| raise RequestError(413, 'Request body is too large.') | |
| if self.headers.get('Content-Type', '').split(';')[0] != 'application/json': | |
| raise RequestError(415, 'Use application/json.') | |
| self.connection.settimeout(10) | |
| try: | |
| body = json.loads(self.rfile.read(length), object_pairs_hook=_object, parse_constant=lambda value: (_ for _ in ()).throw(ValueError('Non-finite JSON number.'))) | |
| except (ValueError, UnicodeError, RecursionError): | |
| raise RequestError(400, 'Request body must be valid JSON without duplicate keys or non-finite numbers.') | |
| if not isinstance(body, dict): | |
| raise RequestError(400, 'Request body must be a JSON object.') | |
| if self.path in {'/api/session', '/api/demo', '/api/twins'} and body: | |
| raise RequestError(400, 'This endpoint accepts an empty object only.') | |
| if self.path == '/api/session': | |
| self.send(201, application.create()) | |
| elif self.path in {'/api/action', '/api/notebook'}: | |
| status, receipt = application.command(self.path.rsplit('/', 1)[1], body) | |
| self.send(status, receipt) | |
| elif self.path == '/api/demo': | |
| self.send(200, application.demonstration()) | |
| else: | |
| self.send(200, application.twins()) | |
| except RequestError as exc: | |
| self.send(exc.status, {'error': exc.message}) | |
| except Exception as exc: | |
| print(f'Borderland request failed: {type(exc).__name__}', file=sys.stderr, flush=True) | |
| self.send(500, {'error': 'The operation could not finish. Try again or restart the local server.'}) | |
| server = ThreadingHTTPServer((host, port), Handler) | |
| allowed_origins = {deployment.public_origin} if deployment.public_origin else { | |
| f'http://127.0.0.1:{server.server_port}', f'http://localhost:{server.server_port}', | |
| } | |
| server.daemon_threads = True | |
| return server | |
| def serve(host='127.0.0.1', port=8765, archive_workload='standard', generation_version=3, notebook_bytes=768, memory_profile='static'): | |
| server = make_server(host, port, application=Application(archive_workload=archive_workload, generation_version=generation_version, notebook_bytes=notebook_bytes, memory_profile=memory_profile)) | |
| print(f'Borderland is running at http://{host}:{server.server_port}', flush=True) | |
| try: | |
| server.serve_forever() | |
| except KeyboardInterrupt: | |
| pass | |
| finally: | |
| server.server_close() | |