feat: v0.5.0 审计修复版 — 类型基线重建 + 会话隔离 + SubAgent 可观测性 + 三项功能补全
CI / 类型检查 + Lint + 单元测试 (push) Failing after 5m38s
CI / 产物编译验证 (push) Successful in 10m15s
CI / 全量测试 (Electron ABI) (push) Failing after 5m27s

P0 安全与工程基线(止血):
- .npmrc 移除硬编码 Gitea npm 凭据,改为 GITEA_NPM_AUTH 环境变量注入(已验证未设变量时 401)
- 修复 typecheck 空操作缺陷:solution-style 根 tsconfig 改为双工程真检查(node + web),
  pre-commit 与 CI 门禁恢复拦截能力
- 修复 4 处 v0.4.1 遗留类型错误:confirmation-hook.test 枚举名 FILE_SYSTEM→FILESYSTEM、
  agent.ts VALIDATION 事件 severity 类型谓词收窄、ContextMenu.tsx 导出 attachments 类型
- 补装 v0.4.1 声明但未安装的 node-html-parser 依赖

P1 逻辑缺陷修复(跨模块边界):
- ConfirmationHook 会话隔离:rememberedDecisions 与 pendingConfirmations 按 sessionId 隔离,
  abortSession 只清本会话 pending(修复 A 会话中断误杀 B 会话确认、拒绝记忆跨会话污染)
- SubAgent 可观测性:orchestrator 六个事件此前全项目零消费者,现接入
  ① subagent:event 生命周期广播(AgentMonitor 新增 SubAgent 状态区)
  ② SubEngine 流事件独立 TRACE 录制(sessionId=taskId 的 JSONL 文件)
- main.ts 启动链路异常兜底:初始化失败时记录日志 + 系统错误对话框 + 退出(原为白屏挂起)

P2 工程强化:
- CI:typecheck 双工程真检查;electron-test 从 experimental(continue-on-error)转正为阻塞门禁;
  GITEA_NPM_AUTH secret 注入说明
- 渲染 bundle 代码分割:单 2630KB chunk 拆为 main 557KB + vendor-react/mui/markdown/icons
  (业务代码变更不再使 vendor 缓存失效)
- database 建表 mcp_servers CHECK 直接含 streamable-http(新库不再依赖迁移 6 立即重建)

P3 功能补全:
- DeepSeek 余额显示:新增 llm:getBalance IPC + LLMSettings 余额卡片(复用适配器原死代码 getBalance)
- FTS5 会话内容搜索:messages_fts 虚表 + INSERT/UPDATE/DELETE 触发器实时同步 +
  存量库 rebuild 迁移 + sessions:searchContent IPC + Sidebar 搜索框标题∪内容联合搜索
  (短语转义防 FTS 运算符注入,按会话聚合展示 snippet)
- 审计日志导出:audit:export IPC(JSONL / CSV RFC 4180 转义)+ LogsSettings 导出按钮

文档一致性大扫除:
- README:工具数统一为 28(原 26/27/30 三口径)、handlers.ts→ipc/、录制事件名更正、
  删除虚构的审计导出/归档宣称与 Schema 虚构字段、MCP 三种传输、配置 key 更正、
  项目结构树对齐实际(settings 10 文件/lib 6 文件/react-virtuoso)、clone 地址改为 Gitea、
  新增 GITEA_NPM_AUTH 配置说明、测试数 207
- 架构/构建指南/UI UX/IR 标准 4 份 HTML 设计文档同步修正(工具数、表数 10、
  磁盘文件 2 个现状注记、ipc/*.ts 路径)
- eslint.config.js 与开发规范.md 注释对齐零容忍基线与 better-sqlite3 选型

测试: 199→207 用例(新增 ConfirmationHook 跨会话隔离 5 用例 + FTS5 搜索/审计导出 8 用例)
验证: lint 0 problems / typecheck 双工程 0 errors / test:electron 207 全过 / build 成功
This commit is contained in:
2026-08-21 21:07:01 +08:00
parent 49c9b25538
commit 7e8b4882a0
32 changed files with 3031 additions and 579 deletions
+98 -39
View File
@@ -40,8 +40,14 @@ export class ConfirmationHook implements PreToolHook {
/** 工具定义缓存(由外部设置) */
private toolDefs = new Map<string, MetonaToolDef>();
/** 用户选择记忆(同一会话内不再重复询问)— v0.4.1: 值扩展为 { approved, at } 以支持拒绝记忆 TTL */
private rememberedDecisions = new Map<string, { approved: boolean; at: number }>();
/**
* 用户选择记忆(sessionId → toolName → 决策)
*
* v0.5.0: 从全局 Map 改为按会话隔离的两级 Map — 多会话并发时,
* A 会话的"记住拒绝/批准"不再影响 B 会话(与 PolicyEngine 频率限制的会话隔离策略一致)。
* 值为 { approved, at } 以支持拒绝记忆 TTLv0.4.1)。
*/
private rememberedDecisions = new Map<string, Map<string, { approved: boolean; at: number }>>();
/**
* v0.4.1: 会话内拒绝记忆的 TTL10 分钟)
@@ -63,6 +69,8 @@ export class ConfirmationHook implements PreToolHook {
timer: NodeJS.Timeout;
toolName: string;
expiresAt: number;
/** v0.5.0: 发起确认的会话 IDclearPending 按会话清理的依据) */
sessionId: string;
/** v0.3.2 批量审批:缓存完整请求信息,供 getPendingConfirmations() 重建 ConfirmationRequest */
args?: Record<string, unknown>;
riskLevel?: string;
@@ -119,8 +127,8 @@ export class ConfirmationHook implements PreToolHook {
}
if (enabled) {
this.autoExecuteTools.add(toolName);
// 自动执行时同步清除会话"拒绝"记忆(避免冲突)
this.rememberedDecisions.delete(toolName);
// 自动执行时同步清除所有会话"拒绝"记忆(避免冲突 — 自动执行是跨会话的全局设置
this.clearDenialMemoryAllSessions(toolName);
} else {
this.autoExecuteTools.delete(toolName);
}
@@ -194,9 +202,9 @@ export class ConfirmationHook implements PreToolHook {
if (autoExecute && approved) {
this.setAutoExecute(pending.toolName, true);
}
// 会话内记忆(v0.4.1: 拒绝记忆带时间戳用于 TTL 过期
// 会话内记忆(v0.4.1: 拒绝记忆带时间戳用于 TTLv0.5.0: 按 pending 所属会话写入
if (remember) {
this.rememberedDecisions.set(pending.toolName, { approved, at: Date.now() });
this.decisionsFor(pending.sessionId).set(pending.toolName, { approved, at: Date.now() });
}
this.pendingConfirmations.delete(toolCallId);
}
@@ -242,7 +250,7 @@ export class ConfirmationHook implements PreToolHook {
this.setAutoExecute(pending.toolName, true);
}
if (remember) {
this.rememberedDecisions.set(pending.toolName, { approved, at: Date.now() });
this.decisionsFor(pending.sessionId).set(pending.toolName, { approved, at: Date.now() });
}
}
}
@@ -275,44 +283,57 @@ export class ConfirmationHook implements PreToolHook {
}
/**
* v0.4.1: 获取本会话内记住"拒绝"的工具列表(含剩余有效期,供前端展示恢复入口)
* v0.4.1: 获取记住"拒绝"的工具列表(含剩余有效期,供前端展示恢复入口)
* v0.5.0: 按 sessionId 过滤 — 只返回指定会话的拒绝记忆;未指定时聚合所有会话(按剩余时间最长去重)
*
* 拒绝记忆有 TTL(默认 10 分钟),到期自动恢复询问;
* 此方法返回未过期的拒绝记忆,前端可提供"重新询问"按钮主动重置。
*
* @param sessionId 会话 ID(前端应传当前会话;缺省时聚合全部会话)
*/
getRememberedDenials(): Array<{ toolName: string; expiresInSeconds: number }> {
getRememberedDenials(sessionId?: string): Array<{ toolName: string; expiresInSeconds: number }> {
const now = Date.now();
const result: Array<{ toolName: string; expiresInSeconds: number }> = [];
for (const [toolName, decision] of this.rememberedDecisions) {
if (decision.approved) continue;
const elapsed = now - decision.at;
if (elapsed >= ConfirmationHook.DENIAL_TTL_MS) {
// 已过期 — 顺手清理,避免列表返回过期条目
this.rememberedDecisions.delete(toolName);
continue;
// toolName → 剩余秒数(跨会话聚合时取最长剩余时间)
const merged = new Map<string, number>();
for (const [sid, decisions] of this.rememberedDecisions) {
if (sessionId && sid !== sessionId) continue;
for (const [toolName, decision] of decisions) {
if (decision.approved) continue;
const elapsed = now - decision.at;
if (elapsed >= ConfirmationHook.DENIAL_TTL_MS) {
// 已过期 — 顺手清理,避免列表返回过期条目
decisions.delete(toolName);
continue;
}
const remaining = Math.ceil((ConfirmationHook.DENIAL_TTL_MS - elapsed) / 1000);
merged.set(toolName, Math.max(merged.get(toolName) ?? 0, remaining));
}
result.push({
toolName,
expiresInSeconds: Math.ceil((ConfirmationHook.DENIAL_TTL_MS - elapsed) / 1000),
});
}
return result;
return Array.from(merged.entries()).map(([toolName, expiresInSeconds]) => ({
toolName,
expiresInSeconds,
}));
}
/**
* v0.4.1: 重置指定工具的会话内拒绝记忆(恢复询问)
* v0.5.0: sessionId 指定会话;未指定时重置所有会话中该工具的拒绝记忆
* @returns true 表示重置成功(存在该工具的拒绝记忆);false 表示没有可重置的记忆
*/
resetRememberedDenial(toolName: string): boolean {
const decision = this.rememberedDecisions.get(toolName);
if (decision && !decision.approved) {
this.rememberedDecisions.delete(toolName);
return true;
resetRememberedDenial(toolName: string, sessionId?: string): boolean {
let reset = false;
for (const [sid, decisions] of this.rememberedDecisions) {
if (sessionId && sid !== sessionId) continue;
const decision = decisions.get(toolName);
if (decision && !decision.approved) {
decisions.delete(toolName);
reset = true;
}
}
return false;
return reset;
}
async beforeExecute(toolCall: MetonaToolCall, _sessionId: string): Promise<HookResult> {
async beforeExecute(toolCall: MetonaToolCall, sessionId: string): Promise<HookResult> {
const def = this.toolDefs.get(toolCall.name);
if (!def) {
// 未知工具,放行(由 ToolRegistry 处理未知工具错误)
@@ -332,14 +353,15 @@ export class ConfirmationHook implements PreToolHook {
return { blocked: false };
}
// 检查是否有记住的决策(v0.4.1: 拒绝记忆带 TTL,过期后恢复询问)
const remembered = this.rememberedDecisions.get(toolCall.name);
if (remembered !== undefined) {
// 检查本会话记住的决策(v0.4.1: 拒绝记忆带 TTL,过期后恢复询问v0.5.0: 按会话隔离
const sessionDecisions = this.rememberedDecisions.get(sessionId);
const remembered = sessionDecisions?.get(toolCall.name);
if (sessionDecisions && remembered !== undefined) {
const isExpiredDenial =
!remembered.approved && Date.now() - remembered.at > ConfirmationHook.DENIAL_TTL_MS;
if (isExpiredDenial) {
// 拒绝记忆已过期 — 移除并继续走正常确认流程
this.rememberedDecisions.delete(toolCall.name);
sessionDecisions.delete(toolCall.name);
} else {
if (remembered.approved) return { blocked: false };
return {
@@ -366,7 +388,7 @@ export class ConfirmationHook implements PreToolHook {
};
// 等待用户响应(带超时)
const approved = await this.waitForConfirmation(request);
const approved = await this.waitForConfirmation(request, sessionId);
if (!approved) {
return { blocked: true, reason: `User denied execution of tool "${toolCall.name}"` };
@@ -385,7 +407,7 @@ export class ConfirmationHook implements PreToolHook {
*/
private lastTimeoutToastAt = 0;
private waitForConfirmation(request: ConfirmationRequest): Promise<boolean> {
private waitForConfirmation(request: ConfirmationRequest, sessionId: string): Promise<boolean> {
return new Promise<boolean>((resolve) => {
const expiresAt = Date.now() + this.confirmationTimeoutMs;
@@ -430,6 +452,7 @@ export class ConfirmationHook implements PreToolHook {
timer,
toolName: request.toolName,
expiresAt,
sessionId,
// v0.3.2 批量审批:同步缓存完整请求信息,供 getPendingConfirmations() 返回
args: request.args,
riskLevel: request.riskLevel,
@@ -447,13 +470,49 @@ export class ConfirmationHook implements PreToolHook {
}
/**
* 清理所有等待中的确认(会话结束时调用)
* 清理等待中的确认(会话中断时调用)
*
* v0.5.0: 支持按会话清理 — 多会话并发时,中断 A 会话只拒绝 A 的 pending
* 不再误杀 B 会话等待中的确认请求(原实现全局清空)。
*
* @param sessionId 指定会话 ID 时只清理该会话的 pending;缺省时清空全部(紧急路径)
*/
clearPending(): void {
for (const [, pending] of this.pendingConfirmations) {
clearPending(sessionId?: string): void {
if (sessionId === undefined) {
for (const [, pending] of this.pendingConfirmations) {
clearTimeout(pending.timer);
pending.resolve(false);
}
this.pendingConfirmations.clear();
return;
}
for (const [id, pending] of this.pendingConfirmations) {
if (pending.sessionId !== sessionId) continue;
clearTimeout(pending.timer);
pending.resolve(false);
this.pendingConfirmations.delete(id);
}
}
// ===== 私有辅助(v0.5.0: 会话隔离) =====
/** 获取(或创建)指定会话的决策记忆表 */
private decisionsFor(sessionId: string): Map<string, { approved: boolean; at: number }> {
let decisions = this.rememberedDecisions.get(sessionId);
if (!decisions) {
decisions = new Map();
this.rememberedDecisions.set(sessionId, decisions);
}
return decisions;
}
/** 清除所有会话中指定工具的拒绝记忆(setAutoExecute 启用时调用) */
private clearDenialMemoryAllSessions(toolName: string): void {
for (const decisions of this.rememberedDecisions.values()) {
const decision = decisions.get(toolName);
if (decision && !decision.approved) {
decisions.delete(toolName);
}
}
this.pendingConfirmations.clear();
}
}