/** Local, offline acceptance using the server's exported image and read-only source evidence. */ import { execFileSync, spawnSync } from 'node:child_process'; import { readFileSync, existsSync, mkdirSync, writeFileSync } from 'node:fs'; import { resolve, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; import { createHash } from 'node:crypto'; const root = resolve(dirname(fileURLToPath(import.meta.url)), '..'); const evidenceArg = process.argv.find((arg) => arg.startsWith('--evidence='))?.slice(11) || process.env.FMODE_RUNTIME_EVIDENCE; if (!evidenceArg) throw new Error('请通过 --evidence=<只读源码目录> 指定服务器模板与运行时取证目录。'); const evidence = resolve(evidenceArg); for (const relative of ['cron/scheduler.py', 'cron/scheduler_script.py', 'cron/scheduler_tick.py', 'cron/jobs.py', 'tools/skills_tool.py', 'agent/skill_utils.py', 'template/plugins/plugin-active-reporter/__init__.py', 'template/scripts/fmode-process-notice.py', 'data/scripts/fmode-deliverable-hook.py']) { if (!existsSync(resolve(evidence, relative))) throw new Error(`缺少只读取证文件:${relative}`); } const baseline = JSON.parse(readFileSync(resolve(root, 'cloud/skills/container-provision-job/container-baseline-v1.json'), 'utf8')); const image = baseline.runtime.image; const inspect = JSON.parse(execFileSync('docker', ['image', 'inspect', image], { encoding: 'utf8' }))[0]; const recipe = readFileSync(resolve(root, 'cloud/skills/container-provision-job/SKILL.md'), 'utf8'); const installBlock = recipe.match(/install -d -o 10000 -g 10000 -m 0755 ([\s\S]*?)\n\n/)?.[1]; if (!installBlock) throw new Error('缺少经过验证的运行目录初始化块'); const directories = [...installBlock.matchAll(/"\$DST(\/[a-z/]+)"/g)].map(match => match[1]); if (!directories.length) throw new Error('运行目录清单为空'); const bootstrap = `import os,sys,subprocess home='/opt/data/provisioned-fixture' os.mkdir(home) os.chown(home,10000,10000) subprocess.run(['install','-d','-o','10000','-g','10000','-m','0755']+[home+p for p in ${JSON.stringify(directories)}],check=True) os.environ['FMODE_PROVISIONED_FIXTURE']=home os.setgroups([]) os.setgid(10000) os.setuid(10000) os.execv(sys.executable,[sys.executable,'-B','-m','unittest','discover','-s','cloud/scripts','-p','test_*.py']) `; const name = `fmode-baseline-local-${Date.now()}`; const args = ['run', '--rm', '--name', name, '--network', 'none', '--memory', baseline.runtime.memoryLimit, '--cpus', String(baseline.runtime.nanoCpus / 1e9), '--pids-limit', String(baseline.runtime.pidsLimit), '--user', '0:0', '--mount', `type=bind,source=${root},target=/repo,readonly`, '--mount', `type=bind,source=${evidence},target=/evidence,readonly`, '--tmpfs', '/opt/data:rw,uid=10000,gid=10000,mode=0700', '-w', '/repo', '-e', 'PYTHONDONTWRITEBYTECODE=1', '-e', 'HERMES_HOME=/opt/data', '--entrypoint', '/opt/hermes/.venv/bin/python', image, '-B', '-c', bootstrap]; const start = Date.now(); const result = spawnSync('docker', args, { cwd: root, encoding: 'utf8', timeout: 180000, maxBuffer: 8 * 1024 * 1024 }); const out = resolve(root, 'test-results/container-runtime'); mkdirSync(out, { recursive: true }); const log = (result.stdout || '') + (result.stderr || ''); writeFileSync(resolve(out, 'runtime.log'), log); writeFileSync(resolve(out, 'receipt.json'), JSON.stringify({ at: new Date().toISOString(), baselineVersion: baseline.version, image, localImageId: inspect.Id, rootfsSha256: createHash('sha256').update(JSON.stringify(inspect.RootFS.Layers)).digest('hex'), network: 'none', user: '10000:10000', memory: baseline.runtime.memoryLimit, cpus: baseline.runtime.nanoCpus / 1e9, pidsLimit: baseline.runtime.pidsLimit, elapsedMs: Date.now() - start, exitCode: result.status, error: result.error?.message, }, null, 2) + '\n'); process.stdout.write(log); if (result.error || result.status !== 0) { // Only the uniquely named local test container can be removed after a timeout. spawnSync('docker', ['rm', '-f', name], { encoding: 'utf8' }); process.exitCode = result.status || 1; }