204 lines
15 KiB
JavaScript
204 lines
15 KiB
JavaScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { createHash } from 'node:crypto';
|
|
import { listFiles, safePath, readText, excludedFromInventory } from '../server/files.mjs';
|
|
import { runGit } from './git-process.mjs';
|
|
import { inspectPythonProject } from './python-project.mjs';
|
|
|
|
const hash = value => createHash('sha256').update(value).digest('hex');
|
|
const accessIdentity = info => hash(JSON.stringify([info.dev, info.ino, info.mode, info.uid, info.gid, info.nlink]));
|
|
const changedSnapshot = () => Object.assign(new Error('索引核对期间源码、分支或读取范围已变化,请重新刷新;未保存本次派生缓存。'), { status: 409 });
|
|
function gitMarker(root) {
|
|
const filename = path.join(root, '.git');
|
|
try {
|
|
const info = fs.lstatSync(filename);
|
|
if (info.isSymbolicLink()) return 'link';
|
|
return accessIdentity(info) + (info.isFile() && info.size < 4096 ? ':' + hash(fs.readFileSync(filename)) : '');
|
|
} catch (error) { if (error.code === 'ENOENT') return 'absent'; throw error; }
|
|
}
|
|
const languages = { '.js': 'JavaScript', '.mjs': 'JavaScript', '.cjs': 'JavaScript', '.ts': 'TypeScript', '.tsx': 'TypeScript', '.mts': 'TypeScript', '.cts': 'TypeScript', '.jsx': 'JavaScript', '.py': 'Python', '.go': 'Go', '.rs': 'Rust', '.cs': 'C#', '.vue': 'Vue', '.html': 'HTML', '.css': 'CSS' };
|
|
const recognizedScripts = /^(test(?::[a-z0-9_-]+)?|typecheck|check|lint|build)$/i;
|
|
|
|
async function gitCommand(root, args) {
|
|
try {
|
|
const result = await runGit(root, args, { timeoutMs: 5000, maximumBytes: 512000 });
|
|
return { ok: result.ok, output: result.text, error: result.ok ? null : result.code };
|
|
} catch (error) { return { ok: false, output: '', error: error.code ?? 'git-unavailable' }; }
|
|
}
|
|
|
|
export async function gitBaseline(project) {
|
|
const root = safePath(project);
|
|
const probe = await gitCommand(root, ['rev-parse', '--is-inside-work-tree']);
|
|
if (!probe.ok || probe.output.trim() !== 'true') return { available: false, head: null, branch: null, dirty: false, status: '', complete: true, paths: null };
|
|
const top = await gitCommand(root, ['rev-parse', '--show-toplevel']);
|
|
const normalize = value => process.platform === 'win32' ? path.resolve(value).toLowerCase() : path.resolve(value);
|
|
if (!top.ok || normalize(top.output.trim()) !== normalize(root)) return { available: false, parentRepository: true,
|
|
note: '上级目录的Git仓库不作为当前项目独立基线。', head: null, branch: null, dirty: false, status: '', complete: true, paths: null };
|
|
// Even status may run a repository's clean/process filter. Read only the
|
|
// configuration key names, then disable those commands for this inspection.
|
|
const filterConfig = await gitCommand(root, ['config', '--null', '--name-only', '--get-regexp', '^filter\\..*\\.(clean|process|required)$']);
|
|
const filtersKnown = filterConfig.ok || filterConfig.error === 1;
|
|
const drivers = [...new Set(filterConfig.output.split('\0').filter(key => /^filter\..+\.(clean|process|required)$/i.test(key)).map(key => key.replace(/\.(clean|process|required)$/i, '')))];
|
|
const filterOverrides = drivers.flatMap(key => ['-c', key + '.clean=', '-c', key + '.process=', '-c', key + '.required=false']);
|
|
const [status, head, branch, files] = await Promise.all([
|
|
filtersKnown ? gitCommand(root, [...filterOverrides, 'status', '--porcelain=v1', '-z', '--untracked-files=all', '--ignore-submodules=all', '--', '.'])
|
|
: Promise.resolve({ ok: false, output: '', error: 'filter-config-unavailable' }),
|
|
gitCommand(root, ['rev-parse', '--verify', 'HEAD']),
|
|
gitCommand(root, ['symbolic-ref', '--quiet', '--short', 'HEAD']),
|
|
gitCommand(root, ['ls-files', '-z', '--cached', '--others', '--exclude-standard', '--', '.']),
|
|
]);
|
|
return { available: true, head: head.ok ? head.output.trim() : null, branch: branch.ok ? branch.output.trim() : null,
|
|
dirty: !!status.output, status: status.output.replaceAll('\0', '\n').slice(0, 8000),
|
|
statusSha256: hash(status.output), complete: status.ok && files.ok,
|
|
paths: files.ok ? [...new Set(files.output.split('\0').filter(Boolean))] : null,
|
|
contentFilters: filtersKnown ? drivers.length ? 'disabled-for-inspection' : 'none' : 'unverified', submoduleWorktrees: 'not-inspected' };
|
|
}
|
|
|
|
function declaration(project, relative, maximum = 64000) {
|
|
const file = safePath(project, relative, true);
|
|
if (!fs.existsSync(file)) return null;
|
|
if (!fs.statSync(file).isFile() || fs.statSync(file).size > maximum) throw new Error(relative + ' 超过工程声明的读取上限。');
|
|
return readText(project, relative);
|
|
}
|
|
|
|
function framework(script) {
|
|
if (/^\s*(?:node|node\.exe)\s+(?:--[^\s]+\s+)*--test(?:\s|$)/.test(script)) return 'node';
|
|
if (/^\s*(?:npx\s+)?vitest(?:\s|$)/.test(script)) return 'vitest';
|
|
if (/^\s*(?:npx\s+)?jest(?:\s|$)/.test(script)) return 'jest';
|
|
return null;
|
|
}
|
|
|
|
export async function inspectProject(project, { includeFiles = false, strictSnapshot = false } = {}) {
|
|
const root = safePath(project), rootAccess = strictSnapshot ? accessIdentity(fs.lstatSync(root)) : null,
|
|
marker = strictSnapshot ? gitMarker(root) : null, git = await gitBaseline(project);
|
|
const inventory = git.paths ? null : listFiles(project, '', 8, 1500);
|
|
const candidates = (git.paths ?? inventory.files.filter(file => file.kind === 'file').map(file => file.path)).slice().sort();
|
|
const fingerprints = [], names = [], excluded = [], detected = new Set(), sourceFiles = [];
|
|
let bytes = 0, complete = git.complete && !inventory?.truncated && candidates.length <= 1000;
|
|
for (const relative of candidates.slice(0, 1000)) {
|
|
if (excludedFromInventory(relative)) { excluded.push(relative); continue; }
|
|
try {
|
|
if (strictSnapshot) {
|
|
let component = root;
|
|
for (const part of relative.replaceAll('\\', '/').split('/')) {
|
|
component = path.join(component, part);
|
|
if (fs.existsSync(component) && fs.lstatSync(component).isSymbolicLink()) throw new Error('索引来源经过链接,不能使用旧解析材料。');
|
|
}
|
|
}
|
|
const file = safePath(project, relative, true);
|
|
if (!fs.existsSync(file)) { fingerprints.push([relative, 'missing']); sourceFiles.push({ path: relative, missing: true, sha256: null, bytes: 0 }); continue; }
|
|
const info = fs.lstatSync(file);
|
|
if (!info.isFile() || info.isSymbolicLink()) { excluded.push(relative); continue; }
|
|
names.push(relative);
|
|
const language = languages[path.extname(relative).toLowerCase()]; if (language) detected.add(language);
|
|
if (info.size > 1024 * 1024 || bytes + info.size > 16 * 1024 * 1024) { complete = false; fingerprints.push([relative, info.size, 'outside-read-budget']); continue; }
|
|
const contents = fs.readFileSync(file), sha256 = hash(contents); bytes += contents.length; fingerprints.push([relative, sha256]);
|
|
sourceFiles.push({ path: relative, sha256, bytes: contents.length, language: language ?? null,
|
|
...(strictSnapshot ? { access: accessIdentity(info) } : {}) });
|
|
} catch (error) {
|
|
excluded.push(relative);
|
|
if (strictSnapshot || error.message !== '该路径不在可读取的项目范围内。') { complete = false; fingerprints.push([relative, 'unreadable']); }
|
|
}
|
|
}
|
|
const readDeclaration = relative => {
|
|
const value = declaration(project, relative), source = sourceFiles.find(file => file.path === relative);
|
|
if (strictSnapshot && value && source?.sha256 && value.sha256 !== source.sha256) throw changedSnapshot();
|
|
return value;
|
|
};
|
|
const packageFile = readDeclaration('package.json');
|
|
let manifest = null;
|
|
if (packageFile) {
|
|
try { manifest = JSON.parse(packageFile.content); } catch { throw new Error('项目package.json不是有效JSON,先修复工程声明。'); }
|
|
if (!manifest || typeof manifest !== 'object' || Array.isArray(manifest)) throw new Error('项目package.json需要一个JSON对象。');
|
|
}
|
|
const fileExists = relative => fs.existsSync(safePath(project, relative, true));
|
|
const packageManager = (typeof manifest?.packageManager === 'string' ? manifest.packageManager.split('@')[0] : null) || (fileExists('pnpm-lock.yaml') ? 'pnpm' : fileExists('yarn.lock') ? 'yarn' : fileExists('bun.lock') || fileExists('bun.lockb') ? 'bun' : packageFile ? 'npm' : 'unknown');
|
|
const scripts = Object.entries(manifest?.scripts ?? {}).filter(([name, script]) => recognizedScripts.test(name) && typeof script === 'string' && script.length <= 6000)
|
|
.map(([name, script]) => ({ name, script, kind: /^test(?::|$)/i.test(name) ? 'test' : name === 'build' ? 'build' : 'check', framework: framework(script),
|
|
lifecycle: ['pre' + name, 'post' + name].filter(key => typeof manifest.scripts[key] === 'string').map(key => ({ name: key, script: manifest.scripts[key].slice(0, 6000) })) }));
|
|
const rules = [];
|
|
for (const relative of names.filter(name => /(^|\/)AGENTS\.md$/i.test(name)).slice(0, 8)) {
|
|
const file = readDeclaration(relative); if (file) rules.push({ path: relative, sha256: file.sha256, excerpt: file.content.slice(0, 1800), interpretation: 'project-data' });
|
|
}
|
|
const declarations = {};
|
|
for (const relative of ['pyproject.toml', 'Cargo.toml', 'go.mod', 'tsconfig.json']) {
|
|
const file = readDeclaration(relative); if (file) declarations[relative] = { sha256: file.sha256, excerpt: file.content.slice(0, 1000) };
|
|
}
|
|
const python = inspectPythonProject(project, sourceFiles, readDeclaration);
|
|
const fingerprint = hash(JSON.stringify({ projectId: project.id, root, files: fingerprints, packageSha256: packageFile?.sha256 ?? null,
|
|
...(python ? { pythonDeclarationHashes: python.declarationHashes } : {}), head: git.head, gitStatus: git.statusSha256 ?? null, complete }));
|
|
const sourceFingerprint = hash(JSON.stringify({ projectId: project.id, root, files: fingerprints,
|
|
packageSha256: packageFile?.sha256 ?? null, ...(python ? { pythonDeclarationHashes: python.declarationHashes } : {}), complete }));
|
|
const { paths, ...visibleGit } = git;
|
|
return { projectId: project.id, name: typeof manifest?.name === 'string' ? manifest.name.slice(0, 150) : project.name,
|
|
languages: [...detected].sort(), packageManager, scripts, rules, declarations, ...(python ? { python } : {}),
|
|
entryPoints: [manifest?.main, typeof manifest?.bin === 'string' ? manifest.bin : null, names.includes('src/index.ts') ? 'src/index.ts' : null, names.includes('src/index.mjs') ? 'src/index.mjs' : null].filter(value => typeof value === 'string').slice(0, 5),
|
|
git: visibleGit, fingerprint, sourceFingerprint, inspectedAt: new Date().toISOString(),
|
|
...(includeFiles ? { files: sourceFiles } : {}),
|
|
...(strictSnapshot ? { snapshot: { rootAccess, gitMarker: marker, candidates } } : {}),
|
|
scope: { complete, scannedFiles: fingerprints.length, bytes, excludedFiles: excluded.length, maximumFiles: 1000, maximumBytes: 16 * 1024 * 1024,
|
|
note: '有界快照;Git工程遵循ignore并包含未跟踪源码,跳过依赖、链接和常见凭据路径。' } };
|
|
}
|
|
|
|
// Close the index snapshot without reparsing manifests or repeating unrelated
|
|
// Git status/filter/config inspection. Rehash actual bytes through owned handles.
|
|
export async function verifyIndexSnapshot(project, inspection) {
|
|
const root = safePath(project), snapshot = inspection.snapshot;
|
|
if (!snapshot || accessIdentity(fs.lstatSync(root)) !== snapshot.rootAccess || gitMarker(root) !== snapshot.gitMarker) throw changedSnapshot();
|
|
let candidates;
|
|
if (inspection.git.available) {
|
|
const [files, head, branch] = await Promise.all([
|
|
gitCommand(root, ['ls-files', '-z', '--cached', '--others', '--exclude-standard', '--', '.']),
|
|
gitCommand(root, ['rev-parse', '--verify', 'HEAD']), gitCommand(root, ['symbolic-ref', '--quiet', '--short', 'HEAD']),
|
|
]);
|
|
if (!files.ok || (head.ok ? head.output.trim() : null) !== inspection.git.head || (branch.ok ? branch.output.trim() : null) !== inspection.git.branch) throw changedSnapshot();
|
|
candidates = [...new Set(files.output.split('\0').filter(Boolean))].sort();
|
|
} else {
|
|
const inventory = listFiles(project, '', 8, 1500);
|
|
candidates = inventory.files.filter(file => file.kind === 'file').map(file => file.path).sort();
|
|
if (inspection.scope.complete && inventory.truncated) throw changedSnapshot();
|
|
}
|
|
if (JSON.stringify(candidates) !== JSON.stringify(snapshot.candidates)) throw changedSnapshot();
|
|
const directories = new Set([root]);
|
|
for (const file of inspection.files) {
|
|
if (file.missing) { if (fs.existsSync(path.join(root, file.path))) throw changedSnapshot(); continue; }
|
|
const filename = path.join(root, file.path); let directory = path.dirname(filename);
|
|
while (!directories.has(directory)) {
|
|
const info = fs.lstatSync(directory);
|
|
if (!info.isDirectory() || info.isSymbolicLink()) throw changedSnapshot();
|
|
directories.add(directory); directory = path.dirname(directory);
|
|
}
|
|
let handle;
|
|
try {
|
|
const current = fs.lstatSync(filename);
|
|
if (!current.isFile() || current.isSymbolicLink() || accessIdentity(current) !== file.access || current.size !== file.bytes) throw changedSnapshot();
|
|
handle = fs.openSync(filename, 'r'); const before = fs.fstatSync(handle);
|
|
if (accessIdentity(before) !== file.access || before.size !== file.bytes) throw changedSnapshot();
|
|
const bytes = fs.readFileSync(handle), after = fs.fstatSync(handle), last = fs.lstatSync(filename);
|
|
if (hash(bytes) !== file.sha256 || before.size !== after.size || before.mtimeMs !== after.mtimeMs || before.ctimeMs !== after.ctimeMs
|
|
|| accessIdentity(last) !== file.access || last.mtimeMs !== after.mtimeMs || last.ctimeMs !== after.ctimeMs) throw changedSnapshot();
|
|
} catch { throw changedSnapshot(); }
|
|
finally { if (handle !== undefined) fs.closeSync(handle); }
|
|
}
|
|
if (gitMarker(root) !== snapshot.gitMarker) throw changedSnapshot();
|
|
return { inspectedAt: new Date().toISOString(), scope: inspection.scope, files: inspection.files };
|
|
}
|
|
|
|
export function createCheckList(inspection, names) {
|
|
if (names !== undefined && (!Array.isArray(names) || !names.length || names.length > 6 || names.some(name => typeof name !== 'string'))) throw new Error('检查脚本选择需要1到6个已识别名称。');
|
|
if (inspection.python?.status === 'invalid' && (inspection.packageManager !== 'npm' || names?.some(name => name.startsWith('python:')))) throw new Error(inspection.python.message);
|
|
const npmScripts = inspection.packageManager === 'npm' ? inspection.scripts : [];
|
|
const pythonChecks = inspection.python?.checks ?? [];
|
|
if (!npmScripts.length && !pythonChecks.length) return [];
|
|
const preferred = ['test', 'typecheck', 'check', 'lint', 'build'];
|
|
const selected = names ?? (inspection.packageManager === 'npm'
|
|
? [...(npmScripts.some(script => script.name === 'test') ? ['test'] : npmScripts.filter(script => script.kind === 'test').map(script => script.name).slice(0, 2)), ...preferred.slice(1).filter(name => npmScripts.some(script => script.name === name))].slice(0, 6)
|
|
: pythonChecks.map(check => check.name));
|
|
return [...new Set(selected)].map(name => {
|
|
const python = pythonChecks.find(item => item.name === name); if (python) return structuredClone(python);
|
|
const script = npmScripts.find(item => item.name === name);
|
|
if (!script) throw new Error('这个脚本不在已识别的检查能力中:' + name);
|
|
return { ...script, command: 'npm run ' + name };
|
|
});
|
|
}
|