Files
metona-ai-desktop/electron/harness/tools/built-in/diff-viewer.ts
T
thzxx 3940716dc2
CI / 类型检查 + Lint + 单元测试 (push) Failing after 5m45s
CI / 全量测试 (Electron ABI) (push) Failing after 5m22s
CI / 产物编译验证 (push) Successful in 10m3s
feat: v0.7.0 四阶段全量迭代 — 修复面收口 · 安全纵深 · 架构还债 · 能力演进
P1 修复面收口: v0.6.3 截断自愈推全量(Anthropic/Ollama/非流式/引擎兜底); SSE 上游错误帧检测进重试通道;
clearMessages 摘要游标根治; truncateResult 内联图片白名单统一; 前端四 bug(确认弹窗锁死/MemoryViewer/
Virtuoso Footer/abort 尾部过滤) + reasoning 缓冲跨迭代污染; 托盘通知过滤与新建会话死链接线

P2 安全纵深: MCP 审批闭环(ConfirmationHook×PolicyEngine 联动+重名拒注册); SSRF 收敛 ssrf-guard 共享模块
(web_fetch 双通道校验+重定向终态复检); Electron 加固(preload CJS 化→sandbox:true/CSP/权限白名单/will-navigate);
run_command cmd.exe 白名单通道元字符守门; diff_viewer 10MB 预检; Anthropic thinking 预算下限; Agnes 思考显式关闭

P3 架构还债: OpenAICompatibleAdapter 中间基类收敛四家样板; 错误分类单轨化(删 mapError/getFetchSignal,
超时显式 ETIMEDOUT); PRAGMA user_version 迁移版本化; 死代码清理专项(cn.ts/SHORTCUTS/ContextMenu 分支/
getWindowState/modifiedArgs/sandbox 空壳); i18next 引入; a11y 第一轮; SearXNG 页批量草稿模型统一

P4 能力演进: Ollama pull 可取消/capabilities 探测/num_ctx 实测缓存; UpdateService feed 比对式自动更新
(app:updateCheck IPC + StatusBar 入口); MiMo providerOptions(web_search 服务端工具/strict JSON);
web_fetch extract_mode=markdown(turndown); network.proxyUrl 全局代理(Chromium sessions+undici dispatcher)

测试: 264 → 507 用例(Electron ABI 全绿零跳过), 覆盖引擎压缩管线/重试竞速/MEMORY.md 闸门/file_editor 五操作/
filesystem 七工具实体夹具/git 真实仓库/SSE 错误帧/全线截断自愈/Provider 请求形态矩阵/SSRF 表测/钩子分级矩阵/
OutputValidator 全量/SLO 指标/MCP 安全纯函数/task_manager 链路/渲染层纯域/i18n 桥契约
2026-08-27 17:06:58 +08:00

280 lines
9.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 文件差异对比工具(1 个)
*
* diff_viewer — 对比两个文件或两段文本的差异
*
* 生成 unified diff 格式输出,支持行级差异检测。
* 用于 Agent 在编辑文件前后对比变化,或对比两个配置文件。
*/
import { readFile, stat } from 'fs/promises';
/** v0.6.4: 单文件 diff 的字节上限(与 read_file/write_file 的 10MB 闸门对齐) */
const MAX_DIFF_FILE_BYTES = 10 * 1024 * 1024;
import type { IMetonaTool, ToolExecutionContext } from '../../types/metona-tool';
import type { MetonaToolDef } from '../../../harness/types';
import { MetonaToolCategory, MetonaRiskLevel } from '../../../harness/types';
import {
safeResolvePath,
extractErrorMessage,
decodeBufferWithDetection,
} from './file-guard';
interface DiffLine {
type: 'context' | 'added' | 'removed';
oldLineNo: number | null;
newLineNo: number | null;
content: string;
}
/** 最长公共子序列(LCSdiff 算法 */
function computeDiff(oldLines: string[], newLines: string[]): DiffLine[] {
// 构建 LCS 表(限制内存:大文件截断到 5000 行)
const maxLines = 5000;
const oldSliced = oldLines.slice(0, maxLines);
const newSliced = newLines.slice(0, maxLines);
const sm = oldSliced.length;
const sn = newSliced.length;
// F4-1: 使用 Uint16Array 一维数组替代 Uint32Array,节省一半内存
// LCS 长度最大值为 min(sm, sn) <= 5000,远小于 Uint16Array 上限 65535,安全
const lcs = new Uint16Array((sm + 1) * (sn + 1));
const idx = (i: number, j: number): number => i * (sn + 1) + j;
for (let i = 1; i <= sm; i++) {
for (let j = 1; j <= sn; j++) {
if (oldSliced[i - 1] === newSliced[j - 1]) {
lcs[idx(i, j)] = lcs[idx(i - 1, j - 1)] + 1;
} else {
lcs[idx(i, j)] = Math.max(lcs[idx(i - 1, j)], lcs[idx(i, j - 1)]);
}
}
}
// 回溯生成 diff
const result: DiffLine[] = [];
let i = sm, j = sn;
while (i > 0 || j > 0) {
if (i > 0 && j > 0 && oldSliced[i - 1] === newSliced[j - 1]) {
result.unshift({ type: 'context', oldLineNo: i, newLineNo: j, content: oldSliced[i - 1] });
i--; j--;
} else if (j > 0 && (i === 0 || lcs[idx(i, j - 1)] >= lcs[idx(i - 1, j)])) {
result.unshift({ type: 'added', oldLineNo: null, newLineNo: j, content: newSliced[j - 1] });
j--;
} else {
result.unshift({ type: 'removed', oldLineNo: i, newLineNo: null, content: oldSliced[i - 1] });
i--;
}
}
return result;
}
/** 生成 unified diff 格式字符串 */
function formatUnifiedDiff(diffLines: DiffLine[], oldLabel: string, newLabel: string, contextLines: number = 3): string {
const lines: string[] = [];
lines.push(`--- ${oldLabel}`);
lines.push(`+++ ${newLabel}`);
let oldLine = 1;
let newLine = 1;
let hunkLines: string[] = [];
let hunkStartOld = 1;
let hunkStartNew = 1;
let inHunk = false;
let contextSinceChange = 0;
const flushHunk = () => {
if (hunkLines.length > 0) {
// 移除尾部多余的 context 行
const trimmed: string[] = [...hunkLines];
while (trimmed.length > 0 && trimmed[trimmed.length - 1].startsWith(' ') && contextSinceChange > 0) {
trimmed.pop();
contextSinceChange--;
}
if (trimmed.length > 0) {
const oldCount = trimmed.filter((l) => l.startsWith('-') || l.startsWith(' ')).length;
const newCount = trimmed.filter((l) => l.startsWith('+') || l.startsWith(' ')).length;
lines.push(`@@ -${hunkStartOld},${oldCount} +${hunkStartNew},${newCount} @@`);
lines.push(...trimmed);
}
}
hunkLines = [];
inHunk = false;
contextSinceChange = 0;
};
for (const dl of diffLines) {
if (dl.type === 'context') {
if (inHunk) {
hunkLines.push(` ${dl.content}`);
contextSinceChange++;
// 超过 contextLines 行连续 context,结束当前 hunk
if (contextSinceChange > contextLines) {
flushHunk();
}
}
oldLine++;
newLine++;
} else if (dl.type === 'added') {
if (!inHunk) {
inHunk = true;
hunkStartOld = oldLine;
hunkStartNew = newLine;
}
hunkLines.push(`+${dl.content}`);
contextSinceChange = 0;
newLine++;
} else if (dl.type === 'removed') {
if (!inHunk) {
inHunk = true;
hunkStartOld = oldLine;
hunkStartNew = newLine;
}
hunkLines.push(`-${dl.content}`);
contextSinceChange = 0;
oldLine++;
}
}
flushHunk();
return lines.join('\n');
}
export class DiffViewerTool implements IMetonaTool {
readonly definition: MetonaToolDef = {
name: 'diff_viewer',
description: 'Compare two files or two text snippets and show differences. Generates unified diff format output. Useful for reviewing changes before applying or comparing configurations.',
parameters: {
type: 'object',
properties: {
mode: {
type: 'string',
description: 'Comparison mode',
enum: ['files', 'text'],
},
file_a: { type: 'string', description: 'First file path (for files mode)' },
file_b: { type: 'string', description: 'Second file path (for files mode)' },
text_a: { type: 'string', description: 'First text content (for text mode)' },
text_b: { type: 'string', description: 'Second text content (for text mode)' },
context_lines: { type: 'number', description: 'Context lines around changes (default 3, max 10)' },
},
required: ['mode'],
},
category: MetonaToolCategory.FILESYSTEM,
riskLevel: MetonaRiskLevel.SAFE,
requiresPermission: false,
timeoutMs: 15_000,
};
async execute(args: Record<string, unknown>, context: ToolExecutionContext): Promise<unknown> {
try {
const mode = args.mode as 'files' | 'text';
const contextLines = Math.min(10, Math.max(0, (args.context_lines as number) ?? 3));
// D4.5: 校验 mode
if (mode !== 'files' && mode !== 'text') {
return { success: false, error: `Invalid mode: ${mode}. Must be 'files' or 'text'` };
}
let contentA: string;
let contentB: string;
let labelA: string;
let labelB: string;
if (mode === 'files') {
const fileA = args.file_a as string;
const fileB = args.file_b as string;
if (!fileA || !fileB) {
return { success: false, error: 'file_a and file_b are required for files mode' };
}
// F4-1: 用 safeResolvePath 统一路径校验(含 isPathWithinWorkspace + MEMORY.md 拦截)
let pathA: string;
let pathB: string;
try {
pathA = safeResolvePath(fileA, context.workspacePath);
pathB = safeResolvePath(fileB, context.workspacePath);
} catch (error) {
return { success: false, error: extractErrorMessage(error) };
}
try {
// v0.6.4 修复(OOM 预检): files 模式此前没有文件大小闸门 —— 数 GB 的
// 日志文件会被 readFile 全量读进主进程内存。read_file/write_file 均有
// 10MB 上限,diff_viewer 是唯一漏网者,补齐同源限制。
const statA = await stat(pathA);
const statB = await stat(pathB);
if (statA.size > MAX_DIFF_FILE_BYTES || statB.size > MAX_DIFF_FILE_BYTES) {
return {
success: false,
error: `File too large for diff: ${statA.size > MAX_DIFF_FILE_BYTES ? fileA : fileB} exceeds ${MAX_DIFF_FILE_BYTES / (1024 * 1024)}MB limit`,
};
}
// F4-1: 用智能编码检测读取文件(支持 GBK/UTF-16 等非 UTF-8 编码)
const bufferA = await readFile(pathA);
const bufferB = await readFile(pathB);
contentA = decodeBufferWithDetection(bufferA).content;
contentB = decodeBufferWithDetection(bufferB).content;
} catch (err) {
return { success: false, error: `Failed to read files: ${extractErrorMessage(err)}` };
}
labelA = fileA;
labelB = fileB;
} else {
contentA = (args.text_a as string) ?? '';
contentB = (args.text_b as string) ?? '';
labelA = 'text_a';
labelB = 'text_b';
}
const linesA = contentA.split('\n');
const linesB = contentB.split('\n');
const diff = computeDiff(linesA, linesB);
const unifiedDiff = formatUnifiedDiff(diff, labelA, labelB, contextLines);
const addedCount = diff.filter((d) => d.type === 'added').length;
const removedCount = diff.filter((d) => d.type === 'removed').length;
const contextCount = diff.filter((d) => d.type === 'context').length;
// D4.3: similarity 计算使用截断后的长度作为分母
const maxLines = 5000;
const oldSlicedLen = Math.min(linesA.length, maxLines);
const newSlicedLen = Math.min(linesB.length, maxLines);
const oldTruncated = linesA.length > maxLines;
const newTruncated = linesB.length > maxLines;
const truncated = oldTruncated || newTruncated;
// 生成摘要
const summary = {
files_compared: mode === 'files' ? 2 : 0,
lines_added: addedCount,
lines_removed: removedCount,
lines_unchanged: contextCount,
total_changes: addedCount + removedCount,
similarity: (oldSlicedLen + newSlicedLen) > 0
? Math.round((contextCount * 2 / (oldSlicedLen + newSlicedLen)) * 100) / 100
: 1,
truncated,
};
// D4.6: unifiedDiff 大小限制
const MAX_DIFF_CHARS = 50_000; const truncatedDiff = unifiedDiff.length > MAX_DIFF_CHARS
? unifiedDiff.slice(0, MAX_DIFF_CHARS) + '\n... (diff truncated)'
: unifiedDiff;
return {
success: true,
summary,
diff: truncatedDiff,
diff_lines: diff.slice(0, 500),
};
} catch (err) {
// F4-2: 统一错误处理,避免 non-Error 抛出值被 String 强转丢失信息
return { success: false, error: extractErrorMessage(err) };
}
}
}