230 lines
15 KiB
JavaScript
230 lines
15 KiB
JavaScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { createHash, randomUUID } from 'node:crypto';
|
|
import { diffLines } from 'diff';
|
|
import { now } from './store.mjs';
|
|
import { assertChangeSetsResolved, assertProposalUnsealed } from '../shared/contracts/change-sets.mjs';
|
|
import { assertWorkspacePath, assertWorkspacesResolved } from '../shared/contracts/workspaces.mjs';
|
|
import { assertMcpResolved } from '../core/tools/mcp-contract.mjs';
|
|
import { assertWorkersResolved, assertWorkerWriteScope } from '../shared/contracts/workers.mjs';
|
|
|
|
export const hash = bytes => createHash('sha256').update(bytes).digest('hex');
|
|
const ignored = new Set(['node_modules', '.git', '.next', '.data', 'bin', 'obj', 'dist', 'dist-client', 'dist-server', '__pycache__', '.venv', 'venv', '.tox', '.pytest_cache', '.mypy_cache', '.ruff_cache', 'htmlcov']);
|
|
const secretName = /(^|\/)(\.env($|[./])|id_rsa($|[./])|id_ed25519($|[./])|credentials($|[./])|\.ssh($|\/)|\.secrets($|\/))/i;
|
|
const normalize = value => process.platform === 'win32' ? value.toLowerCase() : value;
|
|
const inside = (root, target) => normalize(target) === normalize(root) || normalize(target).startsWith(normalize(root + path.sep));
|
|
const restricted = relative => {
|
|
const portable = relative.replaceAll('\\', '/');
|
|
return secretName.test(portable) || portable.split('/').some(part => part.toLowerCase() === '.git'
|
|
|| process.platform === 'win32' && part !== '.' && /[. ]$/.test(part));
|
|
};
|
|
|
|
export function excludedFromInventory(relative) {
|
|
const portable = relative.replaceAll('\\', '/');
|
|
return secretName.test(portable) || portable.split('/').some(part => ignored.has(part.toLowerCase()) || part.startsWith('.awb-'));
|
|
}
|
|
|
|
export function safePath(project, relative = '', allowMissing = false) {
|
|
assertWorkspacePath(project);
|
|
if (typeof relative !== 'string' || relative.includes('\0') || relative.includes(':') || path.isAbsolute(relative) || path.win32.isAbsolute(relative)) throw new Error('路径必须相对于项目文件夹。');
|
|
const portable = relative.replaceAll('\\', '/');
|
|
if (portable.split('/').includes('..') || restricted(portable)) throw new Error('该路径不在可读取的项目范围内。');
|
|
const root = fs.realpathSync.native(project.root);
|
|
if (normalize(root) !== normalize(project.root)) throw new Error('项目位置已变化,请重新打开项目。');
|
|
const target = path.resolve(root, portable || '.');
|
|
if (!inside(root, target)) throw new Error('路径超出项目范围。');
|
|
let existing = target;
|
|
while (!fs.existsSync(existing)) {
|
|
try { if (fs.lstatSync(existing).isSymbolicLink()) throw new Error('目标经过一个失效的链接,请先核对项目路径。'); }
|
|
catch (error) { if (error.code !== 'ENOENT') throw error; }
|
|
if (!allowMissing) throw new Error('文件或文件夹不存在。');
|
|
const parent = path.dirname(existing);
|
|
if (parent === existing) throw new Error('无法确定目标文件夹。');
|
|
existing = parent;
|
|
}
|
|
const real = fs.realpathSync.native(existing);
|
|
if (!inside(root, real)) throw new Error('链接指向项目以外的位置。');
|
|
// A project-local link or Windows alias must not turn a public path into
|
|
// access to protected Git metadata or credentials, including new files.
|
|
if (restricted(path.relative(root, real))) throw new Error('实际路径指向受保护的项目目录或凭据。');
|
|
const actual = fs.statSync(real);
|
|
// realpath cannot reveal the other names of a hard-linked file. Refuse
|
|
// ambiguous file scope before reading bytes or preparing a replacement.
|
|
if (actual.isFile() && actual.nlink !== 1) throw new Error('硬链接文件有多个路径,无法核对完整作用范围;请使用独立文件副本。');
|
|
return target;
|
|
}
|
|
|
|
export function listFiles(project, relative = '', depth = 4, limit = 700) {
|
|
const start = safePath(project, relative);
|
|
if (!fs.statSync(start).isDirectory()) throw new Error('请选择文件夹。');
|
|
const files = [];
|
|
const queue = [{ absolute: start, depth: 0 }];
|
|
let truncated = false;
|
|
while (queue.length) {
|
|
const current = queue.shift();
|
|
const entries = fs.readdirSync(current.absolute, { withFileTypes: true })
|
|
.sort((a, b) => Number(b.isDirectory()) - Number(a.isDirectory()) || a.name.localeCompare(b.name));
|
|
for (const entry of entries) {
|
|
if (excludedFromInventory(entry.name)) continue;
|
|
const absolute = path.join(current.absolute, entry.name);
|
|
const relativePath = path.relative(project.root, absolute).replaceAll('\\', '/');
|
|
if (entry.isSymbolicLink()) continue;
|
|
const directory = entry.isDirectory();
|
|
const info = directory ? null : fs.statSync(absolute);
|
|
if (!directory && (!info.isFile() || info.nlink !== 1)) continue;
|
|
files.push({ path: relativePath, name: entry.name, kind: directory ? 'directory' : 'file',
|
|
bytes: directory ? 0 : info.size });
|
|
if (files.length >= limit) { truncated = true; break; }
|
|
if (directory && current.depth < Math.min(12, Math.max(0, depth))) queue.push({ absolute, depth: current.depth + 1 });
|
|
}
|
|
if (truncated) break;
|
|
}
|
|
return { files, truncated, scope: '按深度和数量上限读取,跳过依赖、版本目录、链接与常见凭据文件。' };
|
|
}
|
|
|
|
export function readText(project, relative) {
|
|
const target = safePath(project, relative);
|
|
if (!fs.statSync(target).isFile()) throw new Error('这不是常规文件。');
|
|
if (fs.statSync(target).size > 1024 * 1024) throw new Error('文件超过 1 MB 的读取上限。');
|
|
const bytes = fs.readFileSync(target);
|
|
if (bytes.includes(0)) throw new Error('这是二进制文件,无法作为文本打开。');
|
|
const text = new TextDecoder('utf8', { fatal: true }).decode(bytes);
|
|
return { path: relative, content: text, sha256: hash(bytes), bytes: bytes.length, bom: bytes.subarray(0, 3).equals(Buffer.from([0xef, 0xbb, 0xbf])) };
|
|
}
|
|
|
|
export function readLines(project, args) {
|
|
const file = readText(project, args.path);
|
|
const lines = file.content.split('\n');
|
|
const start = Math.max(1, Number(args.start_line) || 1);
|
|
const end = Math.min(lines.length, start + 299, Math.max(start, Number(args.end_line) || start + 199));
|
|
return { path: file.path, start_line: start, end_line: end, total_lines: lines.length,
|
|
content: lines.slice(start - 1, end).join('\n'), sha256: file.sha256, bom: file.bom, truncated: start > 1 || end < lines.length };
|
|
}
|
|
|
|
export function searchFiles(project, args) {
|
|
const query = String(args.query ?? '').slice(0, 200);
|
|
if (!query.trim()) throw new Error('请输入搜索内容。');
|
|
const inventory = listFiles(project, '', 10, 2500);
|
|
const matches = [];
|
|
let scanned = 0, budget = 0;
|
|
for (const file of inventory.files) {
|
|
if (file.kind !== 'file' || file.bytes > 256000) continue;
|
|
if (scanned >= 400 || budget + file.bytes > 8 * 1024 * 1024) { inventory.truncated = true; break; }
|
|
try {
|
|
const content = readText(project, file.path).content;
|
|
scanned++; budget += file.bytes;
|
|
const lines = content.split('\n');
|
|
for (let index = 0; index < lines.length; index++) {
|
|
if ((args.case_sensitive ? lines[index] : lines[index].toLocaleLowerCase()).includes(args.case_sensitive ? query : query.toLocaleLowerCase())) {
|
|
matches.push({ path: file.path, line: index + 1, text: lines[index].slice(0, 500) });
|
|
if (matches.length >= 80) break;
|
|
}
|
|
}
|
|
} catch { continue; }
|
|
if (matches.length >= 80) break;
|
|
}
|
|
return { query, matches, scanned_files: scanned, truncated: inventory.truncated || scanned >= 400 || budget >= 8 * 1024 * 1024 || matches.length >= 80,
|
|
scope: '有数量/体积上限的文本搜索;无结果不表示所有文件都已搜索。' };
|
|
}
|
|
|
|
export function proposeFile(store, session, args, { patch = null, sourceBinding = null } = {}) {
|
|
assertWorkersResolved(store, session.projectId);
|
|
assertWorkerWriteScope(store, session, args.path);
|
|
assertMcpResolved(store, session.projectId);
|
|
assertChangeSetsResolved(store, session.projectId);
|
|
assertWorkspacesResolved(store, session.projectId, { includePrepared: true });
|
|
if (typeof args.path !== 'string' || typeof args.content !== 'string') throw new Error('文件提案需要文本路径和完整文本内容。');
|
|
const project = store.project(session.projectId);
|
|
const target = safePath(project, args.path, true);
|
|
let original = null;
|
|
if (fs.existsSync(target)) original = readText(project, args.path);
|
|
if (Object.hasOwn(args, 'base_sha256') && args.base_sha256 !== (original?.sha256 ?? null)) throw Object.assign(new Error('文件已经在读取后发生变化,请重新读取并生成提案。'), { status: 409 });
|
|
const content = String(args.content ?? '');
|
|
if (Buffer.byteLength(content, 'utf8') > 512000) throw new Error('单次提案不能超过 512 KB。');
|
|
if (content.includes('\0') || !content.isWellFormed()) throw new Error('只能提出有效Unicode文本改动。');
|
|
const baseContent = original ? (original.bom ? '\uFEFF' : '') + original.content : '';
|
|
if (original && baseContent === content) return { status: 'unchanged', path: args.path };
|
|
const parts = diffLines(baseContent, content);
|
|
const change = {
|
|
taskId: session.taskId ?? null, runId: session.runId ?? null,
|
|
id: randomUUID(), path: args.path, reason: String(args.reason ?? '建议修改').slice(0, 600), status: 'pending',
|
|
baseExists: !!original, baseSha256: original?.sha256 ?? null, baseContent,
|
|
...(patch ? { editKind: 'exact-patch', patch } : {}),
|
|
...(sourceBinding ? { sourceBinding: structuredClone(sourceBinding) } : {}),
|
|
content, sha256: hash(Buffer.from(content, 'utf8')), diff: parts, createdAt: now(),
|
|
additions: parts.filter(part => part.added).reduce((sum, part) => sum + (part.count ?? 0), 0),
|
|
deletions: parts.filter(part => part.removed).reduce((sum, part) => sum + (part.count ?? 0), 0),
|
|
};
|
|
session.changes.push(change);
|
|
if (!['running', 'awaiting_approval', 'stopping'].includes(session.status)) session.status = 'needs_review';
|
|
store.save(session);
|
|
return { status: 'awaiting_review', change_id: change.id, path: change.path,
|
|
additions: change.additions, deletions: change.deletions, message: '提案已保存,项目文件尚未改变。等待用户查看差异并应用。' };
|
|
}
|
|
|
|
export function proposePatch(store, session, args) {
|
|
if (typeof args.path !== 'string' || typeof args.base_sha256 !== 'string' || !/^[a-f0-9]{64}$/i.test(args.base_sha256)
|
|
|| typeof args.reason !== 'string' || !Array.isArray(args.edits) || !args.edits.length || args.edits.length > 32) throw new Error('精确补丁需要路径、读取时的SHA256、说明和1到32个替换。');
|
|
const original = readText(store.project(session.projectId), args.path);
|
|
if (original.sha256 !== args.base_sha256.toLowerCase()) throw Object.assign(new Error('文件在读取后已变化,请重新读取并核对补丁锚点。'), { status: 409 });
|
|
const lineEnding = /\r(?!\n)/.test(original.content) ? 'mixed' : original.content.includes('\r\n') ? /(^|[^\r])\n/.test(original.content) ? 'mixed' : 'crlf' : 'lf';
|
|
const format = value => lineEnding === 'crlf' ? value.replace(/\r\n|\n/g, '\r\n') : lineEnding === 'lf' ? value.replaceAll('\r\n', '\n') : value;
|
|
const edits = [];
|
|
for (const edit of args.edits) {
|
|
if (!edit || typeof edit !== 'object' || Array.isArray(edit) || typeof edit.old_text !== 'string' || !edit.old_text.length
|
|
|| typeof edit.new_text !== 'string' || edit.old_text.includes('\0') || edit.new_text.includes('\0')
|
|
|| !edit.old_text.isWellFormed() || !edit.new_text.isWellFormed()) throw new Error('补丁替换需要非空old_text和有效Unicode文本new_text。');
|
|
const before = format(edit.old_text), after = format(edit.new_text), start = original.content.indexOf(before);
|
|
if (start < 0) throw Object.assign(new Error('补丁锚点未找到;请用当前文件的实际文本重新生成。'), { status: 409 });
|
|
if (original.content.indexOf(before, start + 1) >= 0) throw Object.assign(new Error('补丁锚点出现多次,需提供更多上下文以确定唯一位置。'), { status: 409 });
|
|
const end = start + before.length;
|
|
edits.push({ start, end, after, startLine: original.content.slice(0, start).split('\n').length, endLine: original.content.slice(0, end).split('\n').length,
|
|
anchorSha256: hash(Buffer.from(before)), replacementSha256: hash(Buffer.from(after)) });
|
|
}
|
|
edits.sort((a, b) => a.start - b.start);
|
|
if (edits.some((edit, index) => index > 0 && edit.start < edits[index - 1].end)) throw Object.assign(new Error('补丁替换范围重叠,请合并为一个唯一锚点后重新生成。'), { status: 409 });
|
|
let content = original.content;
|
|
for (let index = edits.length - 1; index >= 0; index--) content = content.slice(0, edits[index].start) + edits[index].after + content.slice(edits[index].end);
|
|
if (content === original.content) return { status: 'unchanged', path: args.path, base_sha256: original.sha256 };
|
|
if (original.bom) content = '\uFEFF' + content;
|
|
const patch = { edits: edits.length, lineEnding, preservedBom: original.bom,
|
|
ranges: edits.map(({ startLine, endLine, anchorSha256, replacementSha256 }) => ({ startLine, endLine, anchorSha256, replacementSha256 })) };
|
|
const result = proposeFile(store, session, { path: args.path, base_sha256: original.sha256, content, reason: args.reason }, { patch });
|
|
return { ...result, edit_kind: 'exact-patch', base_sha256: original.sha256, patch,
|
|
message: '精确补丁已成为待审查提案;未写入项目。唯一锚点、原文件哈希与不重叠范围均已核对。' };
|
|
}
|
|
|
|
export function applyChange(store, session, id) {
|
|
assertWorkersResolved(store, session.projectId);
|
|
assertMcpResolved(store, session.projectId);
|
|
assertChangeSetsResolved(store, session.projectId);
|
|
assertWorkspacesResolved(store, session.projectId, { includePrepared: true });
|
|
const change = session.changes.find(item => item.id === id);
|
|
if (!change || change.status !== 'pending') throw new Error('这项改动不处于待审查状态。');
|
|
assertWorkerWriteScope(store, session, change.path);
|
|
assertProposalUnsealed(store, change);
|
|
const project = store.project(session.projectId);
|
|
const target = safePath(project, change.path, true);
|
|
const exists = fs.existsSync(target);
|
|
const actualHash = exists ? readText(project, change.path).sha256 : null;
|
|
if (exists !== change.baseExists || actualHash !== change.baseSha256) {
|
|
change.status = 'conflict'; store.save(session);
|
|
throw Object.assign(new Error('文件已经在其他地方修改。请重新生成提案并核对差异。'), { status: 409 });
|
|
}
|
|
const backupRoot = path.join(store.directory, 'backups', session.id);
|
|
fs.mkdirSync(backupRoot, { recursive: true });
|
|
if (exists) fs.copyFileSync(target, path.join(backupRoot, change.id + '.txt'));
|
|
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
safePath(project, change.path, true);
|
|
const temporary = path.join(path.dirname(target), '.awb-' + change.id + '.tmp');
|
|
try {
|
|
fs.writeFileSync(temporary, change.content, { encoding: 'utf8', flag: 'wx' });
|
|
fs.renameSync(temporary, target);
|
|
} finally { if (fs.existsSync(temporary)) fs.unlinkSync(temporary); }
|
|
change.status = 'applied'; change.appliedAt = now();
|
|
store.invalidateChecks(project.id);
|
|
if (session.status === 'needs_review' && !session.changes.some(item => ['pending', 'conflict'].includes(item.status))) session.status = 'completed';
|
|
store.event(session, { kind: 'change', label: '已应用 ' + change.path, phase: 'completed', output: exists ? '写入前基线检查通过,原文已备份。' : '写入前基线检查通过,已创建新文件。' });
|
|
return change;
|
|
}
|