Files
Agent-Workbench/src/core/python-runtime.mjs
T

159 lines
12 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { createHash, randomUUID } from 'node:crypto';
import { execFile } from 'node:child_process';
import { promisify } from 'node:util';
import { projectEnvironment } from './process-environment.mjs';
const executeFile = promisify(execFile);
const digest = bytes => createHash('sha256').update(bytes).digest('hex');
const normalize = value => process.platform === 'win32' ? path.resolve(value).toLowerCase() : path.resolve(value);
const within = (root, file) => normalize(file) === normalize(root) || normalize(file).startsWith(normalize(root) + path.sep);
const fileIdentity = file => {
const resolved = fs.realpathSync.native(file), info = fs.statSync(resolved);
if (!info.isFile() || info.size > 64 * 1024 * 1024) throw new Error('Python运行文件不是受支持的常规文件。');
return { path: resolved, sha256: digest(fs.readFileSync(resolved)) };
};
export function pythonEnvironment() {
return projectEnvironment({ NO_COLOR: '1', CI: '1', PYTEST_DISABLE_PLUGIN_AUTOLOAD: '1', PYTHONDONTWRITEBYTECODE: '1' });
}
function configurationFor(executable) {
for (const file of [path.join(path.dirname(executable), 'pyvenv.cfg'), path.join(path.dirname(executable), '..', 'pyvenv.cfg')]) {
if (fs.existsSync(file)) return fileIdentity(file);
}
return null;
}
function candidates(project, executable) {
if (executable) return [path.isAbsolute(executable) ? path.resolve(executable) : path.resolve(project.root, executable)];
const values = [];
for (const directory of (process.env.PATH ?? '').split(path.delimiter).filter(Boolean)) {
if (process.platform === 'win32' && /[\\/]Microsoft[\\/]WindowsApps(?:[\\/]|$)/i.test(directory)) continue;
for (const name of process.platform === 'win32' ? ['python.exe', 'python3.exe'] : ['python3', 'python']) values.push(path.join(directory, name));
}
if (process.platform === 'win32' && process.env.LOCALAPPDATA) {
const installed = path.join(process.env.LOCALAPPDATA, 'Programs', 'Python');
if (fs.existsSync(installed)) for (const item of fs.readdirSync(installed, { withFileTypes: true }).filter(item => item.isDirectory() && /^Python\d+/i.test(item.name)).sort((a, b) => b.name.localeCompare(a.name))) values.push(path.join(installed, item.name, 'python.exe'));
}
return [...new Set(values)].filter(file => fs.existsSync(file));
}
const probeCode = 'import json,os,sys; print(json.dumps({"executable":os.path.realpath(sys.executable),"version":sys.version.split()[0],"baseExecutable":os.path.realpath(getattr(sys,"_base_executable",sys.executable))}))';
export async function resolvePythonRuntime(project, { executable, allowProjectProbe = false, signal } = {}) {
const errors = [];
for (const logicalExecutable of candidates(project, executable)) {
try {
const identity = fileIdentity(logicalExecutable), configuration = configurationFor(logicalExecutable);
const insideProject = within(project.root, logicalExecutable) || within(project.root, identity.path);
if (insideProject && !allowProjectProbe) return { status: 'deferred', logicalExecutable, executable: identity.path,
sha256: identity.sha256, configuration, version: null, probe: 'after-check-approval', insideProject: true };
const output = await executeFile(logicalExecutable, ['-I', '-S', '-X', 'utf8', '-c', probeCode],
{ cwd: os.tmpdir(), env: pythonEnvironment(), windowsHide: true, timeout: 5000, maxBuffer: 16000, signal });
const info = JSON.parse(output.stdout.trim()), version = info.version?.split('.').map(Number);
if (version?.[0] !== 3 || version[1] < 10) throw new Error('内置Python检查需要Python 3.10或更新版本。');
if (normalize(info.executable) !== normalize(identity.path)) throw new Error('Python探测返回了不同的解释器。');
const base = fileIdentity(info.baseExecutable);
const libraries = process.platform === 'win32' ? fs.readdirSync(path.dirname(base.path)).filter(name => /^python\d+\.dll$/i.test(name)).sort().map(name => fileIdentity(path.join(path.dirname(base.path), name))) : [];
return { status: 'available', logicalExecutable, executable: identity.path, sha256: identity.sha256, configuration,
version: info.version, baseExecutable: base.path, baseSha256: base.sha256, libraries, insideProject,
probe: insideProject ? 'after-check-approval' : 'isolated-host-probe' };
} catch (error) {
if (signal?.aborted) throw error;
errors.push({ executable: logicalExecutable, message: String(error.message).slice(0, 300) });
}
}
return { status: 'missing', logicalExecutable: executable ?? null, executable: null, version: null, errors: errors.slice(0, 3),
message: '未找到可用的Python 3.10+解释器。可安装Python或设置AGENT_WORKBENCH_PYTHON_PATH,然后重新识别检查。' };
}
export async function bindPythonChecks(project, checks, { executable, runnerPath } = {}) {
if (!checks.some(check => check.engine === 'python')) return checks;
const runtime = await resolvePythonRuntime(project, { executable }), runner = fileIdentity(runnerPath);
return checks.map(check => check.engine === 'python' ? { ...check, python: { ...check.python, runtime, runner } } : check);
}
export async function preparePythonInvocation(check, project, { signal } = {}) {
const planned = check.python.runtime;
if (planned.status === 'missing') throw new Error(planned.message);
const runner = fileIdentity(check.python.runner.path);
if (runner.sha256 !== check.python.runner.sha256) throw Object.assign(new Error('内置Python检查器在计划后变化,请重新识别检查。'), { status: 409 });
if (fileIdentity(planned.logicalExecutable).sha256 !== planned.sha256 || JSON.stringify(configurationFor(planned.logicalExecutable)) !== JSON.stringify(planned.configuration)
|| planned.status === 'available' && [
{ path: planned.baseExecutable, sha256: planned.baseSha256 }, ...(planned.libraries ?? [])
].some(file => fileIdentity(file.path).sha256 !== file.sha256)) {
throw Object.assign(new Error('Python解释器或环境声明在计划后变化,未执行探测或检查。'), { status: 409 });
}
const actual = await resolvePythonRuntime(project, { executable: planned.logicalExecutable, allowProjectProbe: true, signal });
if (actual.status !== 'available') throw new Error(actual.message);
if (actual.sha256 !== planned.sha256 || JSON.stringify(actual.configuration) !== JSON.stringify(planned.configuration)
|| planned.status === 'available' && (actual.version !== planned.version || actual.baseSha256 !== planned.baseSha256 || JSON.stringify(actual.libraries) !== JSON.stringify(planned.libraries))) {
throw Object.assign(new Error('Python解释器或环境声明在计划后变化,请重新识别检查。'), { status: 409 });
}
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'awb-python-'));
if (within(project.root, directory)) throw new Error('Python结果目录必须位于项目之外。');
const executableRunner = path.join(directory, 'runner.py'), resultPath = path.join(directory, 'result.json'), invocationId = randomUUID();
fs.writeFileSync(executableRunner, fs.readFileSync(runner.path), { flag: 'wx' });
const input = Buffer.from(JSON.stringify({ invocationId, root: fs.realpathSync.native(project.root), mode: check.python.mode,
settings: check.python.settings, files: check.python.files }));
const args = ['-I', '-B', '-u', '-X', 'utf8', executableRunner, resultPath];
return { executable: planned.logicalExecutable, args, input, resultPath, directory, mode: check.python.mode,
runtime: actual, runner, invocationId, inputSha256: digest(input), files: check.python.files,
dispose() {
const resolved = path.resolve(directory), temporary = path.resolve(os.tmpdir());
if (!within(temporary, resolved) || !path.basename(resolved).startsWith('awb-python-')) throw new Error('Python临时目录归属需要核对。');
fs.rmSync(resolved, { recursive: true, force: true, maxRetries: 3, retryDelay: 50 });
} };
}
export function readPythonEvidence(invocation, exitCode) {
if (!fs.existsSync(invocation.resultPath)) throw new Error('Python进程未返回结构化结果,日志文字不能代替测试统计。');
if (fs.statSync(invocation.resultPath).size > 2 * 1024 * 1024) throw new Error('Python结构化结果超过读取上限。');
const text = fs.readFileSync(invocation.resultPath, 'utf8'), result = JSON.parse(text);
validatePythonEvidence(result, invocation, exitCode);
return { result, text };
}
export function validatePythonEvidence(result, invocation, exitCode) {
if (result.schemaVersion !== 1 || result.kind !== 'python-check-result' || result.invocationId !== invocation.invocationId
|| result.inputSha256 !== invocation.inputSha256 || result.runnerSha256 !== invocation.runner.sha256 || result.mode !== invocation.mode
|| result.exitCode !== exitCode || result.runtime?.sha256 !== invocation.runtime.sha256
|| result.runtime?.baseSha256 !== invocation.runtime.baseSha256 || result.runtime?.version !== invocation.runtime.version
|| normalize(result.runtime?.executable ?? '') !== normalize(invocation.runtime.executable)) throw new Error('Python结果与实际调用、解释器或封存输入不匹配。');
if (!result.collectorError) {
if (invocation.mode === 'compile') {
const value = result.compilation;
if (!value || value.total !== invocation.files.length || !Number.isSafeInteger(value.compiled) || value.compiled < 0
|| !Array.isArray(value.failures) || value.compiled + value.failures.length !== value.total) throw new Error('Python编译数量无法核对。');
} else {
const value = result.tests, keys = ['total', 'passed', 'failed', 'skipped', 'cancelled'];
if (result.framework !== invocation.mode || !value || keys.some(key => !Number.isSafeInteger(value[key]) || value[key] < 0)
|| value.total !== value.passed + value.failed + value.skipped + value.cancelled
|| !Number.isSafeInteger(result.collectionErrors) || result.collectionErrors < 0
|| !Number.isSafeInteger(result.collectionSkipped) || result.collectionSkipped < 0) throw new Error('Python实际测试数量无法核对。');
}
}
return result;
}
export function pythonResultBinding(invocation) {
return { invocationId: invocation.invocationId, inputSha256: invocation.inputSha256, runner: invocation.runner,
executable: invocation.executable, args: invocation.args, runtime: invocation.runtime, mode: invocation.mode };
}
export function verifyPythonCheckEvidence(store, session, job, result) {
if (result.engine !== 'python') return;
if (!result.evidenceValidated || !result.metricsArtifactId || !result.inputArtifactId || !result.python) throw new Error('Python检查缺少实际结构化证据。');
const metrics = store.owned('artifact', result.metricsArtifactId, session.id), input = store.owned('artifact', result.inputArtifactId, session.id);
if (metrics.kind !== 'check-metrics' || input.kind !== 'check-input' || metrics.taskId !== job.taskId || metrics.runId !== job.runId
|| input.taskId !== job.taskId || input.runId !== job.runId || metrics.sha256 !== result.metricsSha256 || input.sha256 !== result.python.inputSha256) throw new Error('Python检查证据归属或输入散列不匹配。');
const payload = JSON.parse(store.artifactStore.read(input)), collected = JSON.parse(store.artifactStore.read(metrics));
if (payload.invocationId !== result.python.invocationId || payload.mode !== result.python.mode) throw new Error('Python检查封存输入与执行身份不匹配。');
validatePythonEvidence(collected, { ...result.python, files: payload.files }, result.exitCode);
if (JSON.stringify(collected.tests ?? null) !== JSON.stringify(result.tests) || JSON.stringify(collected.compilation ?? null) !== JSON.stringify(result.compilation)) throw new Error('Python检查统计与原收集结果不一致。');
for (const file of [{ path: result.python.runtime.executable, sha256: result.python.runtime.sha256 },
{ path: result.python.runtime.baseExecutable, sha256: result.python.runtime.baseSha256 }, result.python.runner,
result.python.runtime.configuration, ...(result.python.runtime.libraries ?? [])].filter(Boolean)) {
if (fileIdentity(file.path).sha256 !== file.sha256) throw new Error('Python执行环境在检查后变化,需要重新验证。');
}
}