feat: v0.4.0 四阶段迭代 — 安全加固 + 工程基线 + 架构重构 + 双 Provider 扩展

P0 安全修复:
- API Key 加密存储(safeStorage 密钥链,版本化前缀,历史明文平滑兼容)
- 间接提示注入防护(SecurityScanHook 工具结果深扫描,网络工具脱敏/本地工具警示分级)
- error:report IPC 断链修复(渲染进程错误上报落 electron-log + 审计)
- abort 信号贯通工具层(run_command/dev-tools 子进程随会话中断终止)
- run_command 沙箱加固(cd 系统目录/敏感文件读取拦截 + chcp 前缀剥离防解析退化)
- .env 真实生效(dotenv 回退加载,应用内配置优先)

P1 工程基础:
- ESLint 9 flat config + 全部 34 条存量 warnings 清零(零容忍基线)
- 测试基线 118 用例 11 文件(token/文件防护/权限/沙箱/注入/命令/引擎/注册表/审计链/摘要分层)
- test:electron 双模式(ELECTRON_RUN_AS_NODE 跑 Electron ABI,SQLite 套件全执行)
- SessionRecorder 多会话隔离 + 9 种 TRACE 事件补全(含最终轮 iteration_end)
- Provider 故障转移(重试耗尽/不可重试一次性切换 fallback + 前端通知)
- MCP 真就绪(等待全部连接完成再广播 tools:ready)
- SLO/HealthChecker 真实接入(60s 巡检 + 托盘状态)
- CONFIG_DEFAULTS 单一来源(消除 SEED 双源漂移)

P2 架构升级:
- handlers.ts 1940 行拆分为 13 个 IPC 域模块(防重入注册 + 多窗口广播)
- AgentEngineManager 每会话独立引擎(LRU 30 + adapter 工厂隔离 abort 信号)
- TaskOrchestrator EngineProvider 改造 + abortByParent 联动中断 SubAgent
- 会话摘要分层上下文(session_summaries 滚动摘要 + 截断游标清理防因果污染)
- 消息编辑重发/重新生成(truncateAfter IPC + store 动作 + UI)
- Markdown 导出 / WebSearch 并行抓取(并发 3)/ 记忆 TF 缓存 / 版本构建期注入

P3 能力扩展:
- OpenAI Adapter(o 系列推理模型 reasoning_effort/max_completion_tokens)
- Anthropic Adapter(原生 Messages API:tool_use 块/角色合并/thinking budget/图片 base64/SSE 事件机)
- 设置页/Onboarding 六 Provider 全链路接入
This commit is contained in:
2026-08-20 23:17:02 +08:00
parent b9f7ec5118
commit 2230bcec3f
90 changed files with 6581 additions and 2771 deletions
+47 -16
View File
@@ -201,6 +201,24 @@ export class MemoryManager {
}
}
/**
* P2-12: 从 tf_cache 列读取缓存的分词结果;缓存缺失/损坏时回退实时分词
*
* tf_cache 在 store() 写入(JSON 序列化的 token 数组),避免每次检索对
* 全部候选文档重复执行 CJK bigram 正则分词(记忆量上千条时明显退化)。
*/
private cachedTokens(cache: string | null | undefined, docText: string): string[] {
if (cache) {
try {
const t = JSON.parse(cache) as unknown;
if (Array.isArray(t) && t.every((x) => typeof x === 'string')) return t as string[];
} catch {
// 缓存损坏 → 回退实时分词
}
}
return tokenize(docText);
}
/**
* L-5 修复: 提取 scoreAndPushMemory 辅助函数
*
@@ -208,14 +226,15 @@ export class MemoryManager {
* 将分数 > 0 的记忆 push 到 results 数组。
*
* 三种记忆类型(episodic/semantic/working)的评分逻辑统一调用此函数,
* 仅在调用前构造 docText/createdAt/importance 等参数。
* 仅在调用前构造 docTokens/createdAt/importance 等参数。
* P2-12: docText → docTokens(分词结果由调用方通过 tf_cache 提供,避免重复分词)
*
* @param params - 评分参数
* @param results - 结果数组(push 到此数组)
*/
private scoreAndPushMemory(
params: {
docText: string;
docTokens: string[];
createdAt: number;
importance: number;
id: string;
@@ -231,8 +250,7 @@ export class MemoryManager {
now: number,
results: SearchResult[],
): void {
const docTokens = tokenize(params.docText);
const docTF = computeTF(docTokens);
const docTF = computeTF(params.docTokens);
const docNorm = vectorNorm(docTF, this.idfCache);
if (docNorm === 0) return;
@@ -284,11 +302,12 @@ export class MemoryManager {
`).all(minImportance, topK * 3) as Array<{
id: string; session_id: string | null; content: string; summary: string | null;
source: string; importance: number; created_at: number; expires_at: number | null;
tf_cache: string | null;
}>;
for (const row of rows) {
this.scoreAndPushMemory({
docText: row.content + ' ' + (row.summary ?? ''),
docTokens: this.cachedTokens(row.tf_cache, row.content + ' ' + (row.summary ?? '')),
createdAt: row.created_at,
importance: row.importance,
id: row.id, type: 'episodic', content: row.content,
@@ -308,11 +327,12 @@ export class MemoryManager {
`).all(minImportance, Math.ceil(topK * 1.5)) as Array<{
id: string; key: string; value: string; category: string | null;
confidence: number; source_session: string | null; created_at: number;
tf_cache: string | null;
}>;
for (const row of rows) {
this.scoreAndPushMemory({
docText: row.key + ' ' + row.value,
docTokens: this.cachedTokens(row.tf_cache, row.key + ' ' + row.value),
createdAt: row.created_at,
importance: row.confidence,
id: row.id, type: 'semantic', content: row.value,
@@ -329,11 +349,12 @@ export class MemoryManager {
`).all(topK * 3) as Array<{
id: string; session_id: string; task_id: string;
key: string; value: string; updated_at: number;
tf_cache: string | null;
}>;
for (const row of rows) {
this.scoreAndPushMemory({
docText: row.key + ' ' + row.value,
docTokens: this.cachedTokens(row.tf_cache, row.key + ' ' + row.value),
createdAt: row.updated_at,
importance: 0.5,
id: row.id, type: 'working', content: row.value,
@@ -363,9 +384,13 @@ export class MemoryManager {
switch (item.type) {
case 'episodic':
db.prepare(`
INSERT INTO episodic_memories (id, session_id, content, summary, source, importance, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
`).run(id, item.sessionId ?? null, item.content, item.summary ?? null, item.source, importance, now);
INSERT INTO episodic_memories (id, session_id, content, summary, source, importance, created_at, tf_cache)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
`).run(
id, item.sessionId ?? null, item.content, item.summary ?? null, item.source, importance, now,
// P2-12: 写入时预计算分词缓存,加速后续检索
JSON.stringify(tokenize(item.content + ' ' + (item.summary ?? ''))),
);
break;
case 'semantic':
// v0.3.0 修复:使用 summary 作为 key(若提供),支持更新已有语义记忆
@@ -373,17 +398,23 @@ export class MemoryManager {
// v0.3.0 用 id 作为 key 时,因 id 每次新生成,INSERT OR REPLACE 永远不触发 REPLACE
// 导致重复 store 同一内容会创建多条记忆。改为 contentHash 后,相同内容自动 REPLACE。
db.prepare(`
INSERT OR REPLACE INTO semantic_memories (id, key, value, category, confidence, source_session, created_at, updated_at, access_count)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0)
`).run(id, item.summary ?? this.contentHash(item.content), item.content, 'general', importance, item.sessionId ?? null, now, now);
INSERT OR REPLACE INTO semantic_memories (id, key, value, category, confidence, source_session, created_at, updated_at, access_count, tf_cache)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, ?)
`).run(
id, item.summary ?? this.contentHash(item.content), item.content, 'general', importance, item.sessionId ?? null, now, now,
JSON.stringify(tokenize((item.summary ?? '') + ' ' + item.content)),
);
break;
case 'working':
// v0.3.0 修复:使用 summary 作为 key(若提供),避免硬编码 'default' 导致覆盖
// #32 修复: 当 summary 未提供时,使用 content hash 作为 key 实现基于内容的去重
db.prepare(`
INSERT OR REPLACE INTO working_memories (id, session_id, task_id, key, value, updated_at)
VALUES (?, ?, ?, ?, ?, ?)
`).run(id, item.sessionId ?? 'default', 'default', item.summary ?? this.contentHash(item.content), item.content, now);
INSERT OR REPLACE INTO working_memories (id, session_id, task_id, key, value, updated_at, tf_cache)
VALUES (?, ?, ?, ?, ?, ?, ?)
`).run(
id, item.sessionId ?? 'default', 'default', item.summary ?? this.contentHash(item.content), item.content, now,
JSON.stringify(tokenize((item.summary ?? '') + ' ' + item.content)),
);
break;
default:
// v0.3.0 修复:未知 type 抛错而非静默失败