v0.17.0: 安全加固 + 数据层性能重构 + 死代码清理 + 测试基建
CI / verify (push) Canceled after 0s

安全修复:
- 开启 webSecurity(CORS 改为 webRequest 允许清单精确放行 Ollama 地址)
- 新增 net-guard SSRF 防护:web_fetch/download_file/browser_open 拦截环回/内网/链路本地地址(DNS 解析后校验)
- browser_open 协议白名单(仅 http/https,阻止 file:// 绕过路径安全层)
- git 参数注入防护(用户可控参数禁止 - 开头;git add 强制 -- 分隔)
- 身份文件保护:SOUL.md/AGENT.md/USER.md 工具只读(防提示注入持久化劫持)
- 系统目录硬红线 + 工作空间/白名单不可豁免系统目录
- spawn_task 权限只降不升(封顶于用户设置 subAgentMaxPermission)
- 子代理写类工具接入主 Agent 确认管线 + 完整路径沙箱
- toast 改 textContent、HTML 导出 escapeHtml(XSS 修复)
- Agent 浏览器改用 memory: 内存分区(退出清空 cookie/storage)

数据层重构:
- sql.js 写入改防抖批量落盘(300ms 合并快照 + temp 原子替换 + 退出刷盘)
- Schema 迁移改 PRAGMA user_version 顺序迁移数组
- 消息/设置/轨迹批量写(单事务);SearXNG 配置 13 次写合并为 1 次
- 会话摘要查询(getSessionSummaries/searchSessions 单条 SQL)消除 N+1
- 导出改 getAllSessionsData 一次 IPC 取回全部行

Bug 修复:
- edit_file 替换符污染($&/$1 被特殊解释导致文件写坏)
- truncateToolResult 暴力截断拼接非法 JSON 必然崩溃
- diff 算法 100MB dp 数组 → 前缀/后缀裁剪 + LCS 限额 + 回退
- move_file 跨盘 rename 失败回退 copy+delete
- Ctrl+K 快捷键冲突(双注册);全局错误处理器双注册
- ffmpeg stderr 无限累积 + 帧进度 O(n²) 正则
- 搜索可达性预检只取响应头(Range: bytes=0-0)
- 备份导出逐字节 base64 拼接(O(n²))改 FileReader
- MCP clientInfo 版本硬编码 5.0.0 改真实版本;tools/list 支持 nextCursor 分页
- 看门狗默认值统一为 30 分钟;download_file 超时跟随用户配置

架构改进:
- 主进程工具分发注册表 tool-dispatch.ts(消除 switch 硬编码)
- agent-engine 拆分 result-formatter.ts / tool-parsing.ts(纯函数)
- 文本兜底解析白名单改从注册表派生(补齐 browser_*/diff/spawn_task/mcp_*)
- diff 工具默认启用;MODE_TOOLS 单一事实来源(tools-modal 复用)
- 记忆系统:条目缓存 + 访问统计(hits/last)持久化 + removeById 按 ID 删除
- 度量历史启动恢复 + Metrics 仪表盘接入 JSON/Prometheus 导出
- 子代理模型下拉框打开设置时刷新(此前从未填充)

死代码清理(约 1400 行):
- 删除 context-indexer 整模块、agent-safety 震荡检测/性能报告/依赖图/记忆调优/归档取回
- 删除 context-manager 水印/跳过压缩/自适应窗口/趋势分析/预算分配等未接线函数
- 删除 sanitizeToolArgs(污染 write_file 内容,防注入职责移交主进程安全层)
- infra-service 裁剪为全局错误处理器唯一定义

文档对齐:
- 新增内置 AGENT.md(工作空间同名文件可覆盖)
- README/帮助面板/DEVELOPMENT 移除失实描述(WAL/内部URL拦截/5层防御/并行白名单/Hook 数量)
- 工具数量口径统一 33;安全机制表新增 SSRF/身份保护/子代理权限等 9 项

工程化:
- Vitest + 34 个单元测试(myers-diff/calculator/net-guard/MEMORY.md 格式)
- Gitea Actions CI(typecheck + test + build)
- package.json 新增 typecheck/test 脚本
This commit is contained in:
2026-08-24 07:39:34 +08:00
parent 6b5e42a26d
commit bae993c321
55 changed files with 2873 additions and 2592 deletions
+81
View File
@@ -0,0 +1,81 @@
import { describe, it, expect } from 'vitest';
import { diffLines, buildUnifiedHunks } from '../src/main/myers-diff.js';
describe('diffLines', () => {
it('全同文件返回全 equal', () => {
const ops = diffLines(['a', 'b', 'c'], ['a', 'b', 'c']);
expect(ops.every(op => op.op === 'equal')).toBe(true);
expect(ops).toHaveLength(3);
});
it('纯新增', () => {
const ops = diffLines(['a'], ['a', 'x', 'y']);
const inserts = ops.filter(op => op.op === 'insert');
expect(inserts).toHaveLength(2);
expect(ops.filter(op => op.op === 'equal')).toHaveLength(1);
expect(ops.filter(op => op.op === 'delete')).toHaveLength(0);
});
it('纯删除', () => {
const ops = diffLines(['a', 'x', 'y', 'b'], ['a', 'b']);
expect(ops.filter(op => op.op === 'delete')).toHaveLength(2);
expect(ops.filter(op => op.op === 'insert')).toHaveLength(0);
});
it('中部修改:前后缀裁剪 + LCS 精确差异', () => {
const old = ['h1', 'h2', 'old1', 'old2', 't1', 't2'];
const now = ['h1', 'h2', 'new1', 't1', 't2'];
const ops = diffLines(old, now);
expect(ops.filter(op => op.op === 'delete').map(op => old[op.oldIdx!])).toEqual(['old1', 'old2']);
expect(ops.filter(op => op.op === 'insert').map(op => now[op.newIdx!])).toEqual(['new1']);
// 前后缀 equal 保留
expect(ops[0].op).toBe('equal');
expect(ops[ops.length - 1].op).toBe('equal');
});
it('空文件对比', () => {
expect(diffLines([], ['a'])).toEqual([{ op: 'insert', newIdx: 0 }]);
expect(diffLines(['a'], [])).toEqual([{ op: 'delete', oldIdx: 0 }]);
expect(diffLines([], [])).toEqual([]);
});
it('LCS 识别交叉公共子序列', () => {
const old = ['a', 'b', 'c', 'd'];
const now = ['b', 'd'];
const ops = diffLines(old, now);
expect(ops.filter(op => op.op === 'equal')).toHaveLength(2); // b、d 被识别为公共
expect(ops.filter(op => op.op === 'delete')).toHaveLength(2); // a、c 删除
expect(ops.filter(op => op.op === 'insert')).toHaveLength(0);
});
});
describe('buildUnifiedHunks', () => {
it('无差异返回空 hunks', () => {
const ops = diffLines(['a'], ['a']);
expect(buildUnifiedHunks(ops, ['a'], ['a'], 3).hunks).toEqual([]);
expect(buildUnifiedHunks(ops, ['a'], ['a'], 3).additions).toBe(0);
});
it('生成带 @@ 头的 unified diff hunk', () => {
const old = ['l1', 'l2', 'l3', 'l4', 'l5', 'l6', 'l7'];
const now = ['l1', 'l2', 'l3', 'CHANGED', 'l5', 'l6', 'l7'];
const ops = diffLines(old, now);
const { hunks, additions, deletions } = buildUnifiedHunks(ops, old, now, 3);
expect(hunks).toHaveLength(1);
expect(hunks[0]).toMatch(/^@@ -1,7 \+1,7 @@/);
expect(hunks[0]).toContain('-l4');
expect(hunks[0]).toContain('+CHANGED');
expect(additions).toBe(1);
expect(deletions).toBe(1);
});
it('相距较远的多处修改生成多个 hunks', () => {
const old = Array.from({ length: 30 }, (_, i) => `line${i}`);
const now = [...old];
now[2] = 'mod-a';
now[25] = 'mod-b';
const ops = diffLines(old, now);
const { hunks } = buildUnifiedHunks(ops, old, now, 2);
expect(hunks.length).toBeGreaterThanOrEqual(2);
});
});