"""Fixed Docker CLI adapter. Missing isolation always fails closed.""" import json import os import re import shutil import subprocess import threading import time import uuid from pathlib import Path from src.modules.skill.domain.models import SkillError, canonical, digest from src.modules.execution.contracts import SandboxConfig from src.adapters.sandbox.process_lifecycle import spawn_owned, release_owned class CommandRunner: def __init__(self): self._lock = threading.Lock() self._processes = set() self._closed = threading.Event() def health(self): """Inspect only subprocesses owned by this runner, without guessing progress.""" with self._lock: processes=list(self._processes) return {'observed': True, 'owned': len(processes), 'alive': sum(p.poll() is None for p in processes), 'closed': self._closed.is_set()} def close(self): self._closed.set() with self._lock: for process in list(self._processes): if process.poll() is None: process.kill() process.wait(timeout=5) def __call__(self, argv, *, timeout, cancelled=lambda: False,on_line=None): if self._closed.is_set(): raise SkillError('RUN_CANCELLED', '执行器已停止。', 409) try: process = spawn_owned(argv, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, creationflags=subprocess.CREATE_NO_WINDOW if os.name == 'nt' else 0) except OSError: raise SkillError('ISOLATION_UNAVAILABLE', 'Docker 不可用,不回退宿主执行。', 503) from None with self._lock: self._processes.add(process) if self._closed.is_set(): process.kill() data = bytearray() overflow = threading.Event() def drain(): pending_lines=bytearray() while chunk := os.read(process.stdout.fileno(),8192): if len(data) + len(chunk) > 2 * 1024 * 1024: overflow.set() break data.extend(chunk) if on_line: pending_lines.extend(chunk) while b'\n' in pending_lines: line,_,rest=pending_lines.partition(b'\n');pending_lines[:]=rest try:on_line(line.decode('utf8',errors='replace').rstrip('\r')) except Exception:pass # Telemetry cannot change execution or its receipt. reader = threading.Thread(target=drain, daemon=True) reader.start() deadline = time.monotonic() + timeout code = None try: while process.poll() is None: if cancelled() or self._closed.is_set() or time.monotonic() >= deadline or overflow.is_set(): code = 'RUN_CANCELLED' if cancelled() or self._closed.is_set() else 'SANDBOX_TIMEOUT' process.kill() break time.sleep(0.03) process.wait(timeout=5) reader.join(timeout=2) finally: if process.poll() is None: process.kill() process.wait(timeout=5) process.stdout.close() release_owned(process) with self._lock: self._processes.discard(process) if code or overflow.is_set(): raise SkillError(code or 'SANDBOX_OUTPUT_LIMIT', '沙箱命令中止,需回收容器。', 503) return process.returncode, data.decode('utf-8', errors='replace') class DockerSandbox: execution_mode = 'docker' def __init__(self, root, config: SandboxConfig, *, command=None, executable=None): self.root = Path(root).resolve() self.root.mkdir(parents=True, exist_ok=True) self.config = config self.command = command or CommandRunner() self.executable = executable or shutil.which('docker') self.suite_digest = digest({'tests': [t.model_dump() for t in config.tests], 'health': config.health.model_dump()}) @property def runtime_contract(self): return {'language': 'Java17', 'dependencies': [], 'main_class': self.config.main_class, 'port': self.config.port, 'health': self.config.health.model_dump(), 'api_contract': self.config.api_contract, 'files': 'src/main/java/**/*.java; optional docs/*.md'} @property def skill_ids(self): return self.config.skill_ids def cmd(self, args, cancelled=lambda: False): if cancelled(): raise SkillError('RUN_CANCELLED', '任务已取消,不再发出容器命令。', 409) if not self.executable: raise SkillError('ISOLATION_UNAVAILABLE', '未安装 Docker,不回退宿主执行。', 503) code, output = self.command([self.executable, *args], timeout=self.config.command_seconds, cancelled=cancelled) if code: raise SkillError('SANDBOX_COMMAND_FAILED', '固定容器命令失败。', 503) return output @staticmethod def name(run_id): if not re.fullmatch(r'run-[a-f0-9]{32}', run_id): raise SkillError('INVALID_RUN_ID', '非法任务标识。') return 'zk-' + run_id def preflight(self): info = json.loads(self.cmd(['info', '--format', '{{json .}}'])) if info.get('OSType') != 'linux' or not any('seccomp' in s for s in info.get('SecurityOptions', [])): raise SkillError('ISOLATION_UNAVAILABLE', '需要 Linux 容器与 seccomp。', 503) self.cmd(['image', 'inspect', self.config.image]) def build(self, run_id, source, cancelled): if digest(source['files']) != source['source_digest']: raise SkillError('SOURCE_DIGEST_MISMATCH', '待构建源码摘要不一致。', 409) self.preflight() name = self.name(run_id) folder = self.root / (name + '-' + uuid.uuid4().hex) source_dir, runner_dir = folder / 'input', folder / 'runner' source_dir.mkdir(parents=True) runner_dir.mkdir() # Only Harness-validated relative files may reach this adapter; validate again at IO boundary. for path, content in source['files'].items(): dest = (source_dir / path).resolve() if not dest.is_relative_to(source_dir.resolve()) or dest == source_dir: raise SkillError('PATH_NOT_ALLOWED', '源码路径越界。') dest.parent.mkdir(parents=True, exist_ok=True) dest.write_text(content, encoding='utf-8', newline='\n') (runner_dir / 'probe.py').write_bytes((Path(__file__).parents[2] / 'modules/execution/container' / ('probe_v2.py' if any(t.check!='exact' for t in self.config.tests) else 'probe.py')).read_bytes()) if any(',' in str(p) for p in (source_dir, runner_dir)): raise SkillError('UNSUPPORTED_SANDBOX_ROOT', '挂载目录不能包含逗号。') args = ['run', '--detach', '--rm', '--pull=never', '--name', name, '--network=none', '--read-only', '--cap-drop=ALL', '--security-opt=no-new-privileges', '--memory=512m', '--memory-swap=512m', '--cpus=1', '--pids-limit=128', '--ulimit=nofile=256:256', '--user=65533:65533', '--tmpfs=/work:rw,nosuid,nodev,size=128m,mode=1777', '--tmpfs=/tmp:rw,noexec,nosuid,nodev,size=32m,mode=1777', '--mount', f'type=bind,src={source_dir},dst=/input,readonly', '--mount', f'type=bind,src={runner_dir},dst=/runner,readonly', '--entrypoint=sleep', self.config.image, str(self.config.lease_seconds)] try: self.cmd(args, cancelled) result = json.loads(self.cmd(['exec', name, 'python3', '/runner/probe.py', 'build'], cancelled)) if not result.get('passed'): raise SkillError('JAVA_BUILD_FAILED', 'Java 编译失败。', 422, details={'diagnostics': str(result.get('diagnostics', ''))[:4000]}) if not re.fullmatch(r'[a-f0-9]{64}', result.get('classes_digest', '')): raise SkillError('INVALID_SANDBOX_RECEIPT', '沙箱回执无效。', 503) return {'source_digest': source['source_digest'], 'classes_digest': result['classes_digest'], 'image': self.config.image, 'suite_digest': self.suite_digest, 'config_digest': digest(self.config), 'execution_mode': self.execution_mode, 'container': name, 'stage': 'build', 'passed': True} except BaseException: if not self.cleanup(run_id): raise SkillError('SANDBOX_CLEANUP_UNKNOWN', '容器回收待核对。', 503) from None raise def probe(self, run_id, candidate, stage, cancelled): name = self.name(run_id) if (candidate['container'] != name or candidate['config_digest'] != digest(self.config) or stage not in {'test', 'preview'}): raise SkillError('EVIDENCE_BINDING_MISMATCH', '沙箱绑定不匹配。', 409) if stage == 'test': self.cmd(['exec', '--detach', '--user=65532:65532', name, 'java', '-Xmx128m', '-Djava.io.tmpdir=/tmp', '-cp', '/work/classes', self.config.main_class], cancelled) result = json.loads(self.cmd(['exec', '--user=65534:65534', '--env', 'ZKCODING_PROBE_CONFIG=' + canonical(self.config), name, 'python3', '/runner/probe.py', stage], cancelled)) if result.get('classes_digest') != candidate['classes_digest']: raise SkillError('EVIDENCE_BINDING_MISMATCH', '编译产物发生变化。', 409) if result.get('passed') is not True: raise SkillError('JAVA_TEST_FAILED' if stage == 'test' else 'JAVA_PREVIEW_FAILED', '独立 HTTP 检查未通过。', 422, details={'failures': result.get('failures', [])}) return dict(candidate, stage=stage, passed=True, cases=result['cases'], passed_cases=result['passed_cases'], preview_scope='container-loopback-health' if stage == 'preview' else None) def cleanup(self, run_id): if not self.executable: return True # No container could have been created through this adapter. try: code, output = self.command([self.executable, 'rm', '-f', self.name(run_id)], timeout=10, cancelled=lambda: False) return code == 0 or 'No such container' in output except Exception: return False