feat: v0.7.4 时序语义修正 · 防线实效补漏 · 全量测试翻倍 — 2406 用例 + jsdom 组件测试全量回归
P1 修复面收口: - 超时三态区分(aborted→USER_INTERRUPT / ETIMEDOUT→TIMEOUT / 其余→ERROR), 根治"真实网络超时被误报为用户中断" - 流空闲超时统一(SSE/Ollama/Anthropic 读循环 60s 无数据抛 504 进重试通道) - 同会话并发 sendMessage 防重入(isRunning 守卫)+ 会话存在性预检 + 前置调用移入 try(ERROR+DONE 双事件保证,根治 isStreaming 假死) - 清空审计后 resetChainCache(根治 verifyChain 误报 TAMPERED) - DONE 不再提前清理 TRACE(TERMINATED 统一收尾,补全最终迭代录制) - IME 合成回车不发送(普通 Enter + Cmd/Ctrl+Enter 双分支)+ handleSend 闭包修复 P2 安全纵深: - preload 移除原始 electronAPI 暴露(渲染层零使用,关掉 XSS invoke 任意通道单点风险) - CORS 同源回显根治(仅当前浏览页面 Origin,did-navigate 同步) - MEMORY.md 命令保护正则扩展(括号/$/反引号/< 重定向边界 + 前导路径) - write_file append TOCTOU 统一(open 后 realpath 校验,新文件分支补漏) - 敏感键归一化(authKey 驼峰/连字符命中)+ MCP headers 鉴权值加密落库 - ReDoS 检测共享化(search_files/file_editor 统一拦截) - run_tests/lint_code 升风险 + 需确认 + npx --no-install(执行边界对齐 run_command) - MCP/SearXNG/llm.baseURL/updateFeedUrl 配置类 URL 高危目标校验(IPv6 去括号 + 十六进制映射解析 + 尾点剥离) P3 架构还债: - temperature/maxTokens 热生效(引擎/编排器/SubAgent 三处接线)+ setBatch 单事务落盘 - SessionRecorder flush 竞态根治(flushPromise 等待 + 超限内联落盘 + stopRecording async) - 内存收口(lastConsolidationBySession LRU / subTraces 清理 / 会话删除 disposeEngine) - i18n 全量收口(28 组件 + 353 key 双字典,状态标签改渲染时函数) - 死代码清理(updateTraceStep/HEADER_HEIGHT/void preA/失实注释) - 斜杠菜单 MUI 化 + 删除逻辑收敛 resetSessionState + Blob URL 统一释放 + 用户消息"仅保存"落库(saveMessage 透传前端 id 修复 id 错位) P4 能力演进: - 死循环检测拆分(驻留前置 + 乒乓后置带进度信号,合法交替不误报) - run-lock 30s 超时强制 abort(旧 run 卡死不无限排队) - RETRY 双通道 stream_reset(前端按 run 归属精确清空,根治重试文本重复) - FTS5 trigram 中文子串搜索(迁移 9 版本化 SCHEMA_VERSION=2,≤2 字符 LIKE 回退) - getContextWindow 兜底 1M→128K(未知模型防 413) 测试: - 855 → 2406 用例(+1551,2.8 倍):服务层 +325(含 MemoryManager 51 新用例)、 工具实体 +483、IPC/适配器 +390(含 OpenAI/Anthropic/Ollama 独立套件)、 纯函数表格化 +330;引入 jsdom + @testing-library(14 组件测试文件 249 用例) - 修复 R1(saveMessage id 透传)/ R2(stream_reset 精确归属)两个回归缺陷 - 遗留低危项清零:git-tools 顺序耦合 / web-fetch 真实时间退避 / slo 内存断言 / mcp-security 多余 skipIf / deepseek-balance 命名误导 / 组件 mock 注入脆弱性 版本: 0.7.4; README 同步(工具风险表/版本徽章); 依赖: 移除 @electron-toolkit/preload, 新增 jsdom/@testing-library(devDependencies 不打包) 回归: typecheck 双端 0 错误; ESLint 0/0; Electron ABI 全量 2406/2406 零跳过; 系统 Node 2110 通过 296 跳过(better-sqlite3 ABI)
This commit is contained in:
@@ -168,3 +168,248 @@ describe('WorkspaceService — 时间戳与 reload', () => {
|
||||
expect(svc.getFiles().soul).not.toBe('tampered');
|
||||
});
|
||||
});
|
||||
|
||||
describe('WorkspaceService — appendMemory 分区定位(v0.7.2 根治)', () => {
|
||||
it('中文分区(用户偏好)条目追加到分区末尾而非文件末尾(v0.7.2 缺陷回归)', () => {
|
||||
const wsPath = join(wsRoot, 'ws');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
svc.initialize();
|
||||
|
||||
svc.appendMemory('用户偏好', '第一条');
|
||||
svc.appendMemory('项目上下文', '项目第一条');
|
||||
|
||||
const content = readFileSync(join(wsPath, 'MEMORY.md'), 'utf-8');
|
||||
const userSection = content.slice(
|
||||
content.indexOf('## 用户偏好'),
|
||||
content.indexOf('## 项目上下文'),
|
||||
);
|
||||
// 项目上下文条目不应出现在用户偏好分区内(旧 bug 会创建重复分区头)
|
||||
expect(userSection).toContain('- 第一条');
|
||||
expect(userSection).not.toContain('项目第一条');
|
||||
// 项目分区只有一份(没有重复的 ## 项目上下文)
|
||||
expect(content.match(/## 项目上下文/g)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('已存在分区:多条条目顺序追加(后写在后)', () => {
|
||||
const wsPath = join(wsRoot, 'ws');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
svc.initialize();
|
||||
svc.appendMemory('用户偏好', 'A');
|
||||
svc.appendMemory('用户偏好', 'B');
|
||||
svc.appendMemory('用户偏好', 'C');
|
||||
const content = readFileSync(join(wsPath, 'MEMORY.md'), 'utf-8');
|
||||
const userSection = content.slice(
|
||||
content.indexOf('## 用户偏好'),
|
||||
content.indexOf('## 项目上下文'),
|
||||
);
|
||||
expect(userSection.indexOf('- A')).toBeLessThan(userSection.indexOf('- B'));
|
||||
expect(userSection.indexOf('- B')).toBeLessThan(userSection.indexOf('- C'));
|
||||
});
|
||||
|
||||
it('新分区(不存在)追加到文件末尾并创建分区头', () => {
|
||||
const wsPath = join(wsRoot, 'ws');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
svc.initialize();
|
||||
svc.appendMemory('新增分区', '新内容');
|
||||
const content = readFileSync(join(wsPath, 'MEMORY.md'), 'utf-8');
|
||||
// 新分区位于文件末尾
|
||||
expect(content.trimEnd().endsWith('- 新内容')).toBe(true);
|
||||
expect(content.match(/## 新增分区/g)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('分区名含正则元字符(括号)时不被误转义破坏', () => {
|
||||
const wsPath = join(wsRoot, 'ws');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
svc.initialize();
|
||||
svc.appendMemory('用户偏好(移动端)', '移动端条目');
|
||||
const content = readFileSync(join(wsPath, 'MEMORY.md'), 'utf-8');
|
||||
expect(content).toContain('## 用户偏好(移动端)');
|
||||
expect(content).toContain('- 移动端条目');
|
||||
});
|
||||
|
||||
it('前缀相似分区互不误匹配("用户偏好" vs "用户偏好(旧)")', () => {
|
||||
const wsPath = join(wsRoot, 'ws');
|
||||
mkdirSync(wsPath, { recursive: true });
|
||||
writeFileSync(
|
||||
join(wsPath, 'MEMORY.md'),
|
||||
'# MEMORY.md\n> 创建时间: x\n> 最后更新: y\n> 工作空间: z\n\n## 用户偏好(旧)\n- 旧条目\n\n## 项目上下文\n',
|
||||
'utf-8',
|
||||
);
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
svc.appendMemory('用户偏好', '新条目');
|
||||
const content = readFileSync(join(wsPath, 'MEMORY.md'), 'utf-8');
|
||||
// 新条目追加到"用户偏好"分区(在(旧)分区后创建独立分区)
|
||||
const mainSection = content.slice(content.indexOf('## 用户偏好\n'));
|
||||
expect(mainSection).toContain('- 新条目');
|
||||
expect(content).toContain('## 用户偏好\n');
|
||||
});
|
||||
|
||||
it('appendMemory 更新"最后更新"时间戳行', () => {
|
||||
const wsPath = join(wsRoot, 'ws');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
svc.initialize();
|
||||
svc.appendMemory('用户偏好', 'x');
|
||||
const content = readFileSync(join(wsPath, 'MEMORY.md'), 'utf-8');
|
||||
expect(content).toMatch(/> 最后更新: \d{4}-\d{2}-\d{2}T/);
|
||||
});
|
||||
|
||||
it('MEMORY.md 不存在时 appendMemory 静默返回(不抛错)', () => {
|
||||
const wsPath = join(wsRoot, 'ws');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
expect(() => svc.appendMemory('用户偏好', 'x')).not.toThrow();
|
||||
});
|
||||
|
||||
it('条目内容含换行与 markdown 语法原样保留', () => {
|
||||
const wsPath = join(wsRoot, 'ws');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
svc.initialize();
|
||||
svc.appendMemory('项目上下文', '第一行\n第二行 `code`');
|
||||
const content = readFileSync(join(wsPath, 'MEMORY.md'), 'utf-8');
|
||||
expect(content).toContain('- 第一行\n第二行 `code`');
|
||||
});
|
||||
});
|
||||
|
||||
describe('WorkspaceService — validateMemoryFormat 自愈矩阵', () => {
|
||||
const setup = (content: string): { wsPath: string; svc: WorkspaceService } => {
|
||||
const wsPath = join(wsRoot, `ws_${Math.random().toString(36).slice(2, 8)}`);
|
||||
mkdirSync(wsPath, { recursive: true });
|
||||
writeFileSync(join(wsPath, 'MEMORY.md'), content, 'utf-8');
|
||||
return { wsPath, svc: new WorkspaceService(wsPath) };
|
||||
};
|
||||
|
||||
it('缺失"创建时间"元数据 → 自动补全并返回 false', () => {
|
||||
const { wsPath, svc } = setup('# MEMORY.md\n> 最后更新: x\n> 工作空间: y\n');
|
||||
expect(svc.validateMemoryFormat()).toBe(false);
|
||||
const content = readFileSync(join(wsPath, 'MEMORY.md'), 'utf-8');
|
||||
expect(content).toContain('> 创建时间:');
|
||||
});
|
||||
|
||||
it('缺失"最后更新"元数据 → 自动补全', () => {
|
||||
const { wsPath, svc } = setup('# MEMORY.md\n> 创建时间: x\n> 工作空间: y\n');
|
||||
expect(svc.validateMemoryFormat()).toBe(false);
|
||||
const content = readFileSync(join(wsPath, 'MEMORY.md'), 'utf-8');
|
||||
expect(content).toContain('> 最后更新:');
|
||||
});
|
||||
|
||||
it('缺失"工作空间"元数据 → 自动补全为当前工作空间路径', () => {
|
||||
const { wsPath, svc } = setup('# MEMORY.md\n> 创建时间: x\n> 最后更新: y\n');
|
||||
expect(svc.validateMemoryFormat()).toBe(false);
|
||||
const content = readFileSync(join(wsPath, 'MEMORY.md'), 'utf-8');
|
||||
expect(content).toContain(`> 工作空间: ${wsPath}`);
|
||||
});
|
||||
|
||||
it('缺失核心分区(用户偏好/项目上下文/重要决策)→ 自动补齐全部', () => {
|
||||
const { wsPath, svc } = setup('# MEMORY.md\n> 创建时间: x\n> 最后更新: y\n> 工作空间: z\n');
|
||||
expect(svc.validateMemoryFormat()).toBe(false);
|
||||
const content = readFileSync(join(wsPath, 'MEMORY.md'), 'utf-8');
|
||||
for (const section of ['## 用户偏好', '## 项目上下文', '## 重要决策']) {
|
||||
expect(content).toContain(section);
|
||||
}
|
||||
});
|
||||
|
||||
it('缺"# MEMORY"标题时元数据插入到文件开头(三条元数据均在原内容之前)', () => {
|
||||
const { wsPath, svc } = setup('- 无标题内容\n');
|
||||
expect(svc.validateMemoryFormat()).toBe(false);
|
||||
const content = readFileSync(join(wsPath, 'MEMORY.md'), 'utf-8');
|
||||
// 元数据插入在开头
|
||||
expect(content.indexOf('> 创建时间:')).toBeLessThan(content.indexOf('- 无标题内容'));
|
||||
expect(content.indexOf('> 最后更新:')).toBeLessThan(content.indexOf('- 无标题内容'));
|
||||
expect(content.indexOf('> 工作空间:')).toBeLessThan(content.indexOf('- 无标题内容'));
|
||||
expect(content).toContain('- 无标题内容');
|
||||
});
|
||||
|
||||
it('完整文件(含标题+元数据+核心分区)返回 true 且不改写', () => {
|
||||
const { wsPath, svc } = setup(
|
||||
'# MEMORY.md\n> 创建时间: x\n> 最后更新: y\n> 工作空间: z\n\n## 用户偏好\n\n## 项目上下文\n\n## 重要决策\n',
|
||||
);
|
||||
const before = readFileSync(join(wsPath, 'MEMORY.md'), 'utf-8');
|
||||
expect(svc.validateMemoryFormat()).toBe(true);
|
||||
const after = readFileSync(join(wsPath, 'MEMORY.md'), 'utf-8');
|
||||
expect(after).toBe(before);
|
||||
});
|
||||
|
||||
it('MEMORY.md 不存在时 validateMemoryFormat 返回 false 不抛错', () => {
|
||||
const wsPath = join(wsRoot, 'ws_nofile');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
expect(svc.validateMemoryFormat()).toBe(false);
|
||||
});
|
||||
|
||||
it('自愈后 getFiles().memory 同步为修正后的内容', () => {
|
||||
const { svc } = setup('# MEMORY.md\n> 创建时间: x\n> 最后更新: y\n> 工作空间: z\n');
|
||||
svc.validateMemoryFormat();
|
||||
expect(svc.getFiles().memory).toContain('## 重要决策');
|
||||
});
|
||||
|
||||
it('重复调用自愈幂等:修正后再次验证返回 true', () => {
|
||||
const { svc } = setup('# MEMORY.md\n> 创建时间: x\n> 最后更新: y\n> 工作空间: z\n');
|
||||
expect(svc.validateMemoryFormat()).toBe(false);
|
||||
expect(svc.validateMemoryFormat()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('WorkspaceService — initialize 与路径边界', () => {
|
||||
it('嵌套目录路径自动创建(多级不存在目录)', () => {
|
||||
const wsPath = join(wsRoot, 'a', 'b', 'c');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
const info = svc.initialize();
|
||||
expect(info.isValid).toBe(true);
|
||||
expect(existsSync(join(wsPath, 'SOUL.md'))).toBe(true);
|
||||
});
|
||||
|
||||
it('getPath 返回构造时传入的路径', () => {
|
||||
const wsPath = join(wsRoot, 'ws_path');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
expect(svc.getPath()).toBe(wsPath);
|
||||
});
|
||||
|
||||
it('initialize 重复调用幂等(不抛错、内容不丢失)', () => {
|
||||
const wsPath = join(wsRoot, 'ws_reinit');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
svc.initialize();
|
||||
writeFileSync(join(wsPath, 'SOUL.md'), '# 自定义人格', 'utf-8');
|
||||
svc.initialize();
|
||||
expect(svc.getFiles().soul).toBe('# 自定义人格');
|
||||
});
|
||||
|
||||
it('SOUL.md 自动创建内容包含灵魂定义标题', () => {
|
||||
const wsPath = join(wsRoot, 'ws_soul');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
svc.initialize();
|
||||
const content = readFileSync(join(wsPath, 'SOUL.md'), 'utf-8');
|
||||
expect(content).toContain('# SOUL.md');
|
||||
expect(content).toContain('身份、性格和核心价值观');
|
||||
});
|
||||
|
||||
it('MEMORY.md 模板的工作空间路径占位符被替换', () => {
|
||||
const wsPath = join(wsRoot, 'ws_placeholder');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
svc.initialize();
|
||||
const content = readFileSync(join(wsPath, 'MEMORY.md'), 'utf-8');
|
||||
expect(content).toContain(`> 工作空间: ${wsPath}`);
|
||||
expect(content).not.toContain('__WORKSPACE_PATH__');
|
||||
});
|
||||
|
||||
it('initialize 返回 files 快照(不可变副本)', () => {
|
||||
const wsPath = join(wsRoot, 'ws_snapshot');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
const info = svc.initialize();
|
||||
info.files.soul = 'mutated';
|
||||
expect(svc.getFiles().soul).not.toBe('mutated');
|
||||
});
|
||||
|
||||
it('reload 同步 MEMORY.md 外部修改并触发格式校验', () => {
|
||||
const wsPath = join(wsRoot, 'ws_reload');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
svc.initialize();
|
||||
writeFileSync(join(wsPath, 'MEMORY.md'), '> 创建时间: x\n', 'utf-8');
|
||||
svc.reload();
|
||||
// reload 后内存内容与磁盘一致(自愈补全在 validateMemoryFormat 中写回)
|
||||
expect(svc.getFiles().memory).toContain('> 创建时间: x');
|
||||
});
|
||||
|
||||
it('updateMemoryTimestamp 对不存在的 MEMORY.md 静默返回', () => {
|
||||
const wsPath = join(wsRoot, 'ws_nots');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
expect(() => svc.updateMemoryTimestamp()).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user