feat: v0.7.3 成本收口 · 状态一致 · 死账清理 — Prompt Cache 根治 + SSRF DNS Pinning + 87 用例扩充全量回归
P1 修复面收口: Prompt Cache 根治(日期/记忆/附件三类易变内容出 system 入用户消息 前置块 user-context.ts, system 跨 run 字节级稳定; Anthropic system 块数组化 + cache_control ephemeral 断言, DeepSeek 自动缓存前缀命中 — 多轮对话输入 token 成本降数量级); 编辑重发/重新生成幽灵 Trace 双侧根治(DB truncateMessagesAfter 同步过滤 metadata.traceSteps + 前端 trimTraceStepsByAnchor 镜像, 严格小于锚点 时间戳, 同毫秒等值判废); sessions:deleteMessage 死通道全链路删除(渲染层零调用 + message_count 漂移面); Ollama vision 能力门控全链路(MetonaModelInfo .supportsVision 贯穿 adapter/IPC/store/UI, model-capabilities.ts 三道判定纯函数, 未知保守放行); 记忆固化节流(consolidation-policy 纯函数: 总开关 + 内容门控 [回答>=200字符或存在成功工具调用] + 会话级 10 分钟频率窗口, 三 memory.* 配置键) P2 安全纵深: SSRF DNS Pinning 关闭 rebinding 窗口(ssrf-guard 重构 resolvePublicAddresses 单源; ssrf-dispatcher 以 undici Agent.connect.lookup 钉死校验 IP, TLS SNI 保持原域名, 一次性 dispatcher 用后即毁; 代理激活显式 退化为仅入口校验); web_fetch 重写手动逐跳重定向循环(每跳先校验后连接, 替代 redirect:follow 内核跟跳的中间跳裸奔, 上限 5 跳); http_request 换用 pinned fetch; web_search 可达性预检加固(私有 URL 零请求 + 不跟跳, 3xx 视为 可达); Agent 浏览器 CORS 通配收紧为 Origin 回显 + Vary: Origin; ConfirmationHook.forgetSession 会话终态清理(会话删除/abort 联动/SubAgent 终结三处接线, 根治 rememberedDecisions 泄漏) P3 架构还债: agent.enableReflection 死配置全链路接线(main→shared→引擎→ Orchestrator→设置开关, REFLECTING 状态真实可达); AgentLoopConfig.timeoutMs 死字段删除; MemoryManager.cleanupExpired 挂入健康检查周期(expires_at 回收 管道真实化); buildSafeEnv 收敛 utils/safe-env.ts 单源(run_command 与 MCP stdio 共用, 终结双实现漂移); Trace 生命周期治理(metadata 只保留最近 20 个 run — keepRecentRuns 纯函数; JSONL 录制启动自动清理保留 200 个 + 设置页 手动清理); SLO/健康快照可视化(app:healthSnapshot IPC + 设置页只读卡片 + 审计链一键校验) P4 能力演进: 会话标题 LLM 自动生成(TitleGenerator — 每会话幂等/并发重入复用 同一 Promise/自定义标题不覆盖/失败静默回退, Sidebar 经 config:changed 实时 刷新); MCP 自动重连(5s/15s/60s 退避最多 3 次, reconnecting 状态机, teardownConnection 内部拆除保留簿记 — 用户断开/开关关闭即时取消, 设置页 显示第 N/3 次); 死循环检测 ABAB 乒乓模式(最近4轮 A→B→A→B 交替判定, 补齐 docs 第五章"两状态反复切换"检测契约); i18n 第三阶段(ChatInput/LLMSettings/ OnboardingWizard/MemoryViewer 主链路文案出层, zh-CN + en-US 双字典补齐) 测试: 737 → 824 用例(+87, 新增 8 个测试文件 + 扩展 3 个)。新覆盖: user-context 分组/空值收缩/拼接契约、context-builder 字节级稳定性、Anthropic cache_control 四态、consolidation-policy 九路判定矩阵、ssrf-dispatcher(pinned lookup/重定向 解析/IP 校验)、forget-session 会话隔离、trace-lifecycle run 淘汰、 trace-trim 严格小于边界、safe-env 净化矩阵、mcp-reconnect 退避状态机 (fake timers)、title-generator 并发重入、SQLite 侧 truncate×TRACE 联动 (Electron ABI)。测试驱动修复: GIT_*/ 注释终止块注释、重连计数被自身重试 前置断开重置(拆 teardownConnection 保留簿记)、TitleGenerator 幂等占位与 并发去重的检查顺序竞态(去重先于幂等) 版本: 0.7.3; README 同步(配置表新增 agent.enableReflection/memory.*/mcp.autoReconnect) 回归: typecheck 双端 0 错误; ESLint 0/0; 系统 Node 771 通过 53 跳过 (better-sqlite3 ABI); Electron ABI 全量 824/824 零跳过
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* ConfirmationHook forgetSession 测试(v0.7.3 P2-3)
|
||||
*
|
||||
* 锁定会话终态清理契约:
|
||||
* F1 forgetSession 清空该会话的决策记忆(拒绝记忆不再残留);
|
||||
* F2 forgetSession 同时拒绝该会话等待中的确认(clearPending 语义);
|
||||
* F3 会话隔离:清理 A 不影响 B;
|
||||
* F4 空/未知 sessionId 幂等无副作用。
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
|
||||
// 模拟一个存活窗口 —— beforeExecute 的 hasAvailableWindow 守卫需要它,
|
||||
// 否则确认请求在创建 pending 之前即被短路(测不到记忆/pending 路径)
|
||||
const fakeWindow = {
|
||||
isDestroyed: () => false,
|
||||
webContents: { send: vi.fn() },
|
||||
};
|
||||
vi.mock('electron', () => ({
|
||||
BrowserWindow: { getAllWindows: vi.fn(() => [fakeWindow]) },
|
||||
}));
|
||||
|
||||
import { ConfirmationHook } from '../confirmation-hook';
|
||||
import type { MetonaToolCall, MetonaToolDef } from '../../../harness/types';
|
||||
import { MetonaRiskLevel, MetonaToolCategory } from '../../../harness/types';
|
||||
|
||||
function makeToolCall(name: string, id = `tc_${name}`): MetonaToolCall {
|
||||
return { id, name, args: {}, iteration: 1, timestamp: Date.now() };
|
||||
}
|
||||
|
||||
const NEEDS_CONFIRM_DEF: MetonaToolDef = {
|
||||
name: 'run_command',
|
||||
description: 'test',
|
||||
parameters: { type: 'object', properties: {}, required: [] },
|
||||
category: MetonaToolCategory.CODE_EXECUTION,
|
||||
riskLevel: MetonaRiskLevel.HIGH,
|
||||
requiresPermission: true,
|
||||
timeoutMs: 1000,
|
||||
};
|
||||
|
||||
describe('ConfirmationHook — forgetSession(P2-3)', () => {
|
||||
let hook: ConfirmationHook;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
hook = new ConfirmationHook(null, null);
|
||||
hook.setToolDefs([NEEDS_CONFIRM_DEF]);
|
||||
});
|
||||
|
||||
it('F1: 会话删除后决策记忆被清空(拒绝记忆不再跨生命周期残留)', async () => {
|
||||
const pending = hook.beforeExecute(makeToolCall('run_command'), 'sess-A');
|
||||
hook.resolveConfirmation(`tc_run_command`, false, true); // 记住拒绝
|
||||
await pending;
|
||||
|
||||
expect(hook.getRememberedDenials('sess-A')).toHaveLength(1);
|
||||
hook.forgetSession('sess-A');
|
||||
expect(hook.getRememberedDenials('sess-A')).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('F2: forgetSession 拒绝该会话等待中的确认(clearPending 语义)', async () => {
|
||||
const p1 = hook.beforeExecute(makeToolCall('run_command'), 'sess-A');
|
||||
// resolve pending(拒绝)后走记忆清理路径
|
||||
hook.forgetSession('sess-A');
|
||||
await expect(p1).resolves.toMatchObject({ blocked: true });
|
||||
// pending 已清空 —— getPendingConfirmations 无残留
|
||||
expect(hook.getPendingConfirmations()).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('F3: 会话隔离 —— 清理 A 不影响 B 的决策记忆', async () => {
|
||||
const p1 = hook.beforeExecute(makeToolCall('run_command', 'tc_A'), 'sess-A');
|
||||
hook.resolveConfirmation('tc_A', false, true);
|
||||
await p1;
|
||||
|
||||
const p2 = hook.beforeExecute(makeToolCall('run_command', 'tc_B'), 'sess-B');
|
||||
hook.resolveConfirmation('tc_B', false, true);
|
||||
await p2;
|
||||
|
||||
hook.forgetSession('sess-A');
|
||||
expect(hook.getRememberedDenials('sess-A')).toHaveLength(0);
|
||||
expect(hook.getRememberedDenials('sess-B')).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('F4: 空 sessionId 幂等无副作用;未知会话不抛错', () => {
|
||||
expect(() => hook.forgetSession('')).not.toThrow();
|
||||
expect(() => hook.forgetSession('nonexistent')).not.toThrow();
|
||||
expect(hook.getRememberedDenials()).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
@@ -550,6 +550,22 @@ export class ConfirmationHook implements PreToolHook {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* v0.7.3 P2-3: 会话生命周期终态清理(会话删除 / SubAgent 终结时调用)。
|
||||
*
|
||||
* 此前 rememberedDecisions 两级 Map 只增不减 —— 会话删除/子任务终结后其
|
||||
* 决策记忆永久残留,长期运行实例随会话数缓慢泄漏。本方法与 clearPending
|
||||
* 的区别:clearPending 只处理等待中的确认(会话中断时用,会话本身仍存活),
|
||||
* 本方法面向"会话已终结"的终态,同时清空 pending 与决策记忆。
|
||||
*
|
||||
* @param sessionId 会话 ID(主会话或 SubAgent taskId)
|
||||
*/
|
||||
forgetSession(sessionId: string): void {
|
||||
if (!sessionId) return;
|
||||
this.clearPending(sessionId);
|
||||
this.rememberedDecisions.delete(sessionId);
|
||||
}
|
||||
|
||||
// ===== 私有辅助(v0.5.0: 会话隔离) =====
|
||||
|
||||
/** 获取(或创建)指定会话的决策记忆表 */
|
||||
|
||||
Reference in New Issue
Block a user