Files
Agent-Workbench/src/core/environment/environment-inspector.mjs
T

172 lines
14 KiB
JavaScript

import { unrestricted, ceiling } from '../../shared/contracts/execution-policy.mjs';
import fs from 'node:fs';
import path from 'node:path';
import { createHash } from 'node:crypto';
import { safePath } from '../../server/files.mjs';
import { projectWorkspaceIdentity } from '../../shared/contracts/workspaces.mjs';
import { inspectProject } from '../project-inspector.mjs';
import { resolvePythonRuntime } from '../python-runtime.mjs';
export const environmentHash = value => createHash('sha256').update(typeof value === 'string' || Buffer.isBuffer(value) ? value : JSON.stringify(value)).digest('hex');
export const environmentConflict = message => Object.assign(new Error(message), { status: 409 });
const same = (a,b) => (process.platform === 'win32' ? path.resolve(a).toLowerCase() : path.resolve(a)) === (process.platform === 'win32' ? path.resolve(b).toLowerCase() : path.resolve(b));
export function fileIdentity(file, maximum = 128 * 1024 * 1024) {
const stat = fs.lstatSync(file), resolved = fs.realpathSync.native(file);
if (!stat.isFile() || stat.isSymbolicLink() || stat.nlink !== 1 || stat.size > maximum) throw environmentConflict('环境输入需要大小受限的独立常规文件:' + path.basename(file));
const bytes = fs.readFileSync(file), after = fs.lstatSync(file);
if (stat.ino !== after.ino || stat.mtimeMs !== after.mtimeMs || stat.ctimeMs !== after.ctimeMs || bytes.length !== after.size) throw environmentConflict('读取期间环境输入已变化,请重新诊断。');
return { path: resolved, sha256: environmentHash(bytes), bytes: bytes.length };
}
function declaration(project, name) {
const target = safePath(project, name, true);
try { fs.lstatSync(target); } catch (error) { if (error.code === 'ENOENT') return null; throw error; }
const identity = fileIdentity(target, ceiling(project, 8 * 1024 * 1024)), text = fs.readFileSync(target, 'utf8');
if (environmentHash(text) !== identity.sha256) throw environmentConflict('环境声明在读取期间变化。');
return { ...identity, text };
}
export function npmRuntime(runtimeDirectory = null) {
const cli = [runtimeDirectory, path.dirname(process.execPath)].filter(Boolean)
.map(directory => path.join(directory, 'node_modules', 'npm', 'bin', 'npm-cli.js')).find(file => fs.existsSync(file));
const node = { ...fileIdentity(process.execPath), version: process.version };
if (!cli) return { status: 'missing', node, npm: null, message: '宿主没有固定npm CLI,请使用随包Node启动。' };
const manifest = fileIdentity(path.join(path.dirname(cli), '..', 'package.json'), 1024 * 1024);
const version = JSON.parse(fs.readFileSync(manifest.path, 'utf8')).version;
return { status: 'available', node, npm: { ...fileIdentity(cli), version, manifest } };
}
// Hash actual dependency bytes. A hidden lockfile alone cannot prove that the
// installed packages are still the ones a previous command checked.
export function dependencyTree(project, { maximumFiles = 40000, maximumBytes = 512 * 1024 * 1024 } = {}) {
if (unrestricted(project)) { maximumFiles = Infinity; maximumBytes = Infinity; }
const root = path.join(safePath(project), 'node_modules'), hash = createHash('sha256');
let files = 0, bytes = 0, entriesSeen = 0;
try {fs.lstatSync(root);}catch(error){if(error.code==='ENOENT')return { status:'missing',files:0,bytes:0,sha256:null };throw error;}
const visit = (directory, depth = 0) => {
if (depth > ceiling(project, 100)) throw new Error('依赖目录过深');
const stat = fs.lstatSync(directory);
if (!stat.isDirectory() || stat.isSymbolicLink() || !same(fs.realpathSync.native(directory), directory)) throw new Error('依赖目录包含链接或路径替换');
const entries=fs.readdirSync(directory).sort();
for (const name of entries) {
const target = path.join(directory,name), relative = path.relative(root,target).replaceAll('\\','/'), info = fs.lstatSync(target);
if (++entriesSeen > maximumFiles) throw new Error('依赖文件超过诊断上限');
if (info.isSymbolicLink()) {
const real = fs.realpathSync.native(target), within = path.relative(root,real);
if (path.isAbsolute(within) || within === '..' || within.startsWith('..' + path.sep)) throw new Error('依赖链接指向工程以外');
files++; hash.update(JSON.stringify([relative,'link',fs.readlinkSync(target)]) + '\n'); continue;
}
// Build tools may leave empty temporary directories. Only file/link
// content affects this identity; every directory is still traversed and
// checked for replacement, links and the entry/depth budgets.
if (info.isDirectory()) { visit(target,depth+1); continue; }
files++;
bytes += info.size;
if (!info.isFile() || info.nlink !== 1 || bytes > maximumBytes) throw new Error('依赖类型或大小超出可核验范围');
const input = fileIdentity(target,maximumBytes);
hash.update(JSON.stringify([relative,input.sha256,input.bytes,info.mode & 0o777]) + '\n');
}
if(JSON.stringify(entries)!==JSON.stringify(fs.readdirSync(directory).sort()))throw new Error('读取期间依赖目录发生变化');
};
try { visit(root); return { status: 'verified', files, bytes, sha256: hash.digest('hex') }; }
catch (error) { return { status: 'unverified', files, bytes, sha256: null, message: error.message }; }
}
function json(file, name) {
try { const result = JSON.parse(file.text); if (!result || Array.isArray(result) || typeof result !== 'object') throw new Error(); return result; }
catch { throw environmentConflict(name + '不是有效JSON对象。'); }
}
export function inspectNodeEnvironment(project, { runtimeDirectory = null, treeLimits } = {}) {
const runtime = npmRuntime(runtimeDirectory), manifestFile = declaration(project,'package.json');
if (!manifestFile) return null;
const manifest = json(manifestFile,'package.json'), lockFile = declaration(project,'package-lock.json');
const manager = manifest.packageManager?.split?.('@')[0] ?? (['pnpm-lock.yaml','yarn.lock','bun.lock','bun.lockb'].find(name=>fs.existsSync(safePath(project,name,true))) ? 'other' : 'npm');
const issues = [], lock = lockFile ? json(lockFile,'package-lock.json') : null;
const declarations = [manifestFile,lockFile].filter(Boolean).map(({text,...value})=>value);
const dependencies = Object.fromEntries(Object.entries({ ...manifest.dependencies, ...manifest.devDependencies, ...manifest.optionalDependencies }).sort(([a],[b])=>a.localeCompare(b)));
if (manager !== 'npm') issues.push('当前自动准备仅支持npm,已识别包管理器:' + manager);
if (manifest.workspaces) issues.push('当前自动准备不支持npm workspaces。');
if (fs.existsSync(safePath(project,'npm-shrinkwrap.json',true))) issues.push('检测到npm-shrinkwrap.json,当前准备器只接受package-lock.json。');
if (!lock) issues.push('缺少package-lock.json;请先人工生成并审查锁文件。');
else if (![2,3].includes(lock.lockfileVersion) || !lock.packages || !lock.packages['']) issues.push('准备器需要package-lock v2或v3和完整packages字段。');
const rootLock = lock?.packages?.[''];
if (rootLock) for (const name of ['dependencies','devDependencies','optionalDependencies']) {
const normalized = value => Object.entries(value ?? {}).sort(([a],[b])=>a.localeCompare(b));
if (JSON.stringify(normalized(manifest[name])) !== JSON.stringify(normalized(rootLock[name]))) issues.push('声明与锁文件的' + name + '不一致。');
}
const packages = [];
for (const [relative,value] of Object.entries(lock?.packages ?? {})) {
if (!relative) continue;
if (packages.length >= ceiling(project, 5000)) { issues.push('锁文件包数超过5000的支持范围。'); break; }
if (!/^node_modules\//.test(relative) || relative.includes('\\') || relative.split('/').some(part=>!part||part==='..'||part==='.'||part.includes(':')) || value?.link || !value || typeof value !== 'object') {
issues.push('锁文件含本地链接、工作区或不支持的包路径。'); continue;
}
let source = null;
try {
const url = new URL(value.resolved);
if (url.username || url.password || url.search || url.hash || !/^https?:$/.test(url.protocol)) throw new Error();
source = { origin: url.origin, url: url.href };
} catch { issues.push('锁文件存在未固定的包来源:' + relative); }
if (!/^sha512-[A-Za-z0-9+/]{86}==$/.test(value.integrity ?? '')) issues.push('锁文件缺少sha512完整性:' + relative);
packages.push({ path:relative, version:value.version ?? null, source, integrity:value.integrity ?? null, hasInstallScript:value.hasInstallScript === true, optional:value.optional === true });
}
for (const [name,version] of Object.entries(dependencies)) if (typeof version !== 'string' || /^(?:file:|link:|workspace:|git|https?:|ssh:|github:)|[\\]/i.test(version)) issues.push('不支持本地或未固定依赖声明:' + name);
const installed = dependencyTree(project,treeLimits), missing = [], mismatched = [];
for (const [name] of Object.entries(dependencies)) {
if (!/^(?:@[a-z0-9_.-]+\/)?[a-z0-9_.-]+$/i.test(name)) { issues.push('依赖名称无效。'); continue; }
const packagePath = 'node_modules/' + name + '/package.json';
try { const current = declaration(project,packagePath), info = current && json(current,packagePath);
if (!info) missing.push(name);
else if (packages.find(value=>value.path==='node_modules/'+name)?.version !== info.version) mismatched.push(name);
} catch { mismatched.push(name); }
}
const npmrc = []; for (let directory=safePath(project);;directory=path.dirname(directory)) {
if (fs.existsSync(path.join(directory,'.npmrc'))) npmrc.push(path.join(directory,'.npmrc'));
if (path.dirname(directory)===directory) break;
}
if (npmrc.length) issues.push('项目或父目录有.npmrc;准备器不读取或继承其中凭据,请在无此配置的隔离工作区准备。');
const lifecycle = Object.entries(manifest.scripts ?? {}).filter(([name])=>['preinstall','install','postinstall','prepublish','preprepare','prepare','postprepare'].includes(name))
.map(([name,script])=>({name,script:String(script).slice(0,6000)}));
const inputs = { declarations:declarations.map(({path:file,...value})=>({name:path.basename(file),...value})),
node:{sha256:runtime.node.sha256,version:runtime.node.version}, npm:runtime.npm?{sha256:runtime.npm.sha256,version:runtime.npm.version,manifestSha256:runtime.npm.manifest.sha256}:null };
const ready = !missing.length && !mismatched.length && (!Object.keys(dependencies).length || installed.status==='verified');
return { kind:'node', manager, runtime, declarations, lockVersion:lock?.lockfileVersion ?? null, packageCount:packages.length,
packages, dependencies:Object.keys(dependencies), missing, mismatched, installed, lifecycle, installScriptPackages:packages.filter(value=>value.hasInstallScript).map(value=>value.path),
sources:[...new Set(packages.flatMap(value=>value.source?[value.source.origin]:[]))].sort(),
issues:[...new Set(issues)], preparable:runtime.status==='available' && !issues.length,
status:manager!=='npm'?'unsupported':ready?'ready':'needs_preparation', dependencyInputs:environmentHash(inputs),
identity:environmentHash({inputs,installed,root:safePath(project),runtime,manager,issues:[...new Set(issues)]}), inputs };
}
export function nodeEnvironmentBinding(project, options) {
const node = inspectNodeEnvironment(project,options);
return node ? { kind:'node',identity:node.identity,dependencyInputs:node.dependencyInputs,status:node.status,
installed:node.installed, declarations:node.declarations, runtime:node.runtime } : null;
}
export function verifyNodeBinding(project, binding) {
if (!binding) return;
const current = nodeEnvironmentBinding(project,{runtimeDirectory:binding.runtime?.npm ? path.resolve(binding.runtime.npm.path,'../../../..') : null});
if (!current || current.identity !== binding.identity) throw environmentConflict('检查绑定的运行时、声明或实际依赖已变化,请重新诊断和检查。');
}
async function inspectPythonEnvironment(project, inspection, options) {
if (!inspection.python) return null;
const runtime = await resolvePythonRuntime(project,{executable:options.pythonExecutable ?? process.env.AGENT_WORKBENCH_PYTHON_PATH ?? inspection.python.preferredExecutable});
const declarations = ['pyproject.toml','requirements.txt','requirements-dev.txt','Pipfile.lock','poetry.lock','uv.lock','pytest.ini','setup.cfg']
.map(name=>declaration(project,name)).filter(Boolean).map(({text,...value})=>value);
let pytest = {status:'missing',version:null,metadata:null};
if (runtime.executable) {
const directory = path.dirname(runtime.logicalExecutable), bases = [path.resolve(directory,'..','Lib','site-packages'),path.resolve(directory,'Lib','site-packages')];
if (runtime.version) bases.push(path.resolve(directory,'..','lib','python'+runtime.version.split('.').slice(0,2).join('.'),'site-packages'));
for (const base of bases) {
if (!fs.existsSync(base)) continue;
const matches = fs.readdirSync(base).filter(name=>/^pytest-[\d.]+\.dist-info$/i.test(name));
if (matches.length === 1) { const metadata=fileIdentity(path.join(base,matches[0],'METADATA'),1024*1024);
pytest={status:'available',version:matches[0].slice(7,-10),metadata}; break; }
}
}
return {kind:'python',status:runtime.status,framework:inspection.python.framework,runtime,pytest,declarations,
preparation:'manual',message:'未执行项目代码或导入pytest;本阶段不自动安装Python依赖。项目内解释器的版本探测等待检查审批。'};
}
export async function diagnoseEnvironment(project, options = {}) {
const inspection = await inspectProject(project), node = inspectNodeEnvironment(project,options), python = await inspectPythonEnvironment(project,inspection,options);
return {schemaVersion:1,inspectedAt:new Date().toISOString(),projectId:project.id,root:safePath(project),workspace:projectWorkspaceIdentity(project),
fingerprint:inspection.fingerprint,sourceFingerprint:inspection.sourceFingerprint,scope:inspection.scope,node,python,
status:!node&&!python?'unsupported':node?.status==='needs_preparation'?'needs_preparation':node?.status??python.status};
}