Spaces:
Sleeping
Sleeping
Download src/modules/execution/container/probe.py from dellweb3/VibeCoding: direct link, hf CLI and curl.
- Browser
- Download file 4.44 kB
-
https://huggingface.co/spaces/dellweb3/VibeCoding/resolve/main/src/modules/execution/container/probe.py
- Command line
-
hf download hf://spaces/dellweb3/VibeCoding/src/modules/execution/container/probe.py
-
curl -L -o probe.py https://huggingface.co/spaces/dellweb3/VibeCoding/resolve/main/src/modules/execution/container/probe.py
4.44 kB
| """Platform-owned program inside the container. No model-supplied commands.""" | |
| import hashlib | |
| import json | |
| import os | |
| import pathlib | |
| import subprocess | |
| import sys | |
| import time | |
| import urllib.error | |
| import urllib.request | |
| def classes_digest(): | |
| files = sorted(pathlib.Path('/work/classes').rglob('*')) | |
| return hashlib.sha256(json.dumps({str(p.relative_to('/work/classes')): hashlib.sha256(p.read_bytes()).hexdigest() | |
| for p in files if p.is_file()}, sort_keys=True).encode()).hexdigest() | |
| def emit(value): | |
| print(json.dumps(value), flush=True) | |
| def main(): | |
| stage = sys.argv[1] | |
| if stage == 'build': | |
| sources = sorted(str(p) for p in pathlib.Path('/input/src/main/java').rglob('*.java')) | |
| pathlib.Path('/work/classes').mkdir(mode=0o755) | |
| # Bounded by source/file limits; diagnostics are data, never command instructions. | |
| dependency = os.environ.get('ZKCODING_DEPENDENCY_PROFILE', 'jdk') | |
| classpath = ['-cp', '/approved/pdfbox-app-3.0.8.jar'] if dependency == 'pdfbox-3.0.8' else [] | |
| if dependency not in {'jdk', 'pdfbox-3.0.8'}: | |
| raise ValueError('UNAPPROVED_DEPENDENCY_PROFILE') | |
| result = subprocess.run(['javac', '-J-Xmx256m', '-proc:none', '-encoding', 'UTF-8', '--release', '17', '-d', '/work/classes', *classpath, *sources], | |
| stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=90) | |
| if result.returncode: | |
| emit({'passed': False, 'code': 'JAVA_BUILD_FAILED', 'diagnostics': result.stdout.decode('utf-8', errors='replace')[:4000]}) | |
| return | |
| # The generated Java process runs as a different UID and cannot modify classes. | |
| emit({'passed': True, 'classes_digest': classes_digest()}) | |
| return | |
| # Only this independently owned probe process receives the protected test data. | |
| # The generated service's UID cannot read this process's /proc/.../environ. | |
| if os.environ.get('ZKCODING_PROBE_CONFIG_FILE') == '/runner/probe-config.json': | |
| suite = json.loads(pathlib.Path('/runner/probe-config.json').read_text()) | |
| else: | |
| suite = json.loads(os.environ['ZKCODING_PROBE_CONFIG']) | |
| original = classes_digest() | |
| opener = urllib.request.build_opener(urllib.request.ProxyHandler({}), NoRedirect()) | |
| cases = suite['tests'] if stage == 'test' else [suite['health']] | |
| results = [] | |
| failures = [] | |
| for case in cases: | |
| body = json.dumps(case['body']).encode() if case['body'] is not None else None | |
| request = urllib.request.Request('http://127.0.0.1:' + str(suite['port']) + case['path'], data=body, | |
| method=case['method'], headers={'Content-Type': 'application/json', **case.get('headers', {})}) | |
| received = None | |
| actual_status=None | |
| actual_body=None | |
| for attempt in range(40): | |
| try: | |
| try: | |
| response = opener.open(request, timeout=20 if suite.get('dependency_profile')=='pdfbox-3.0.8' else 2) | |
| except urllib.error.HTTPError as exc: | |
| response = exc | |
| with response: | |
| raw = response.read(65537) | |
| actual_status=response.code | |
| actual_body=raw[:500].decode('utf-8',errors='replace') | |
| received = len(raw) <= 65536 and response.code == case['status'] and json.loads(raw) == case['expected_json'] | |
| break | |
| except (OSError, ValueError): | |
| # Retry only service readiness before the first GET; never repeat POST effects. | |
| if case['method'] != 'GET' or results or attempt == 39: | |
| received = False | |
| break | |
| time.sleep(0.5) | |
| results.append(bool(received)) | |
| if not received: | |
| failures.append({'case_index': len(results) - 1, 'path': case['path'], 'expected_status': case['status'], | |
| 'reason': 'response_did_not_match_public_contract','actual_status':actual_status,'actual_body':actual_body}) | |
| emit({'passed': all(results) and original == classes_digest(), 'classes_digest': original, | |
| 'cases': len(results), 'passed_cases': sum(results), 'failures': failures}) | |
| class NoRedirect(urllib.request.HTTPRedirectHandler): | |
| def redirect_request(self, *args): | |
| return None | |
| if __name__ == '__main__': | |
| main() | |