feat: v0.5.0 审计修复版 — 类型基线重建 + 会话隔离 + SubAgent 可观测性 + 三项功能补全
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:
@@ -24,7 +24,12 @@ import { EventEmitter } from 'events';
|
||||
import { nanoid } from 'nanoid';
|
||||
import { AgentLoopEngine } from '../agent-loop/engine';
|
||||
import type { AgentLoopConfig } from '../agent-loop/types';
|
||||
import type { MetonaMessage, MetonaSystemPrompt, MetonaToolDef, IMetonaProviderAdapter } from '../types';
|
||||
import type {
|
||||
MetonaMessage,
|
||||
MetonaSystemPrompt,
|
||||
MetonaToolDef,
|
||||
IMetonaProviderAdapter,
|
||||
} from '../types';
|
||||
import type { ToolRegistry } from '../tools/registry';
|
||||
import type { PreToolHook } from '../hooks/pre-tool';
|
||||
import type { PostToolHook } from '../hooks/post-tool';
|
||||
@@ -32,12 +37,28 @@ import log from 'electron-log';
|
||||
|
||||
export interface SubAgentResult {
|
||||
taskId: string;
|
||||
/** v0.5.0: 委派方(父会话)ID — 供事件消费者按会话过滤 */
|
||||
parentSessionId: string;
|
||||
result: string;
|
||||
success: boolean;
|
||||
durationMs: number;
|
||||
iterations: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* v0.5.0: SubAgent 生命周期事件载荷(统一广播给前端 AgentMonitor 展示)
|
||||
*/
|
||||
export interface SubAgentEvent {
|
||||
taskId: string;
|
||||
parentSessionId: string;
|
||||
description: string;
|
||||
status: 'delegated' | 'running' | 'completed' | 'error';
|
||||
depth: number;
|
||||
durationMs?: number;
|
||||
iterations?: number;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* P2-10: 引擎供给接口(由 AgentEngineManager 实现)
|
||||
* orchestrator 不再持有单个引擎引用,而是按需创建独立实例。
|
||||
@@ -115,9 +136,12 @@ export class TaskOrchestrator extends EventEmitter {
|
||||
// ===== 递归深度检查 =====
|
||||
const currentDepth = this.sessionDepth.get(params.parentSessionId) ?? 0;
|
||||
if (currentDepth >= MAX_DELEGATION_DEPTH) {
|
||||
log.warn(`[Orchestrator] Delegation depth limit reached (${currentDepth}) for session ${params.parentSessionId}`);
|
||||
log.warn(
|
||||
`[Orchestrator] Delegation depth limit reached (${currentDepth}) for session ${params.parentSessionId}`,
|
||||
);
|
||||
return {
|
||||
taskId,
|
||||
parentSessionId: params.parentSessionId,
|
||||
result: `SubAgent delegation depth limit reached (${MAX_DELEGATION_DEPTH}). Cannot delegate further.`,
|
||||
success: false,
|
||||
durationMs: 0,
|
||||
@@ -127,7 +151,12 @@ export class TaskOrchestrator extends EventEmitter {
|
||||
const depth = currentDepth + 1;
|
||||
this.sessionDepth.set(params.parentSessionId, depth);
|
||||
|
||||
this.emit('taskDelegated', { taskId, description: params.description, parentSessionId: params.parentSessionId, depth });
|
||||
this.emit('taskDelegated', {
|
||||
taskId,
|
||||
description: params.description,
|
||||
parentSessionId: params.parentSessionId,
|
||||
depth,
|
||||
});
|
||||
|
||||
// ===== 创建独立的引擎实例(P2-10: 独立 adapter,隔离 abort 信号) =====
|
||||
const subEngine = new AgentLoopEngine(
|
||||
@@ -147,6 +176,10 @@ export class TaskOrchestrator extends EventEmitter {
|
||||
subEngine.setFallbackAdapter(this.engines.getFallbackAdapter());
|
||||
subEngine.setWorkspacePath(this.engines.getWorkspacePath());
|
||||
|
||||
// v0.5.0: 转发 SubEngine 的流式/状态事件(供 TRACE 录制与前端可观测)
|
||||
// 事件以 taskId 为 sessionId,录制到独立 JSONL 文件,不污染父会话的流
|
||||
this.forwardSubEngineEvents(subEngine, taskId);
|
||||
|
||||
// ===== 工具白名单设置 =====
|
||||
const allowedTools = this.resolveTools(params.tools);
|
||||
subEngine.setTools(allowedTools);
|
||||
@@ -163,11 +196,21 @@ export class TaskOrchestrator extends EventEmitter {
|
||||
handle.status = 'error';
|
||||
this.activeSubAgents.delete(taskId);
|
||||
},
|
||||
getStatus: () => ({ taskId, status: handle.status, description: handle.description, depth: handle.depth }),
|
||||
getStatus: () => ({
|
||||
taskId,
|
||||
status: handle.status,
|
||||
description: handle.description,
|
||||
depth: handle.depth,
|
||||
}),
|
||||
};
|
||||
|
||||
this.activeSubAgents.set(taskId, handle);
|
||||
this.emit('taskStarted', { taskId, depth });
|
||||
this.emit('taskStarted', {
|
||||
taskId,
|
||||
description: params.description,
|
||||
parentSessionId: params.parentSessionId,
|
||||
depth,
|
||||
});
|
||||
|
||||
try {
|
||||
// 构建用户消息
|
||||
@@ -184,7 +227,7 @@ export class TaskOrchestrator extends EventEmitter {
|
||||
const output = await subEngine.runStream(
|
||||
userMessage,
|
||||
taskId,
|
||||
[], // 子 Agent 无历史
|
||||
[], // 子 Agent 无历史
|
||||
systemPrompt,
|
||||
);
|
||||
|
||||
@@ -192,6 +235,7 @@ export class TaskOrchestrator extends EventEmitter {
|
||||
const success = output.terminationReason === 'completed';
|
||||
const result: SubAgentResult = {
|
||||
taskId,
|
||||
parentSessionId: params.parentSessionId,
|
||||
result: output.finalAnswer,
|
||||
success,
|
||||
durationMs,
|
||||
@@ -202,7 +246,9 @@ export class TaskOrchestrator extends EventEmitter {
|
||||
handle.result = result;
|
||||
this.emit('taskCompleted', result);
|
||||
|
||||
log.info(`[Orchestrator] SubAgent "${taskId}" (depth=${depth}) ${success ? 'completed' : 'failed'} in ${durationMs}ms, ${output.iterations.length} iterations`);
|
||||
log.info(
|
||||
`[Orchestrator] SubAgent "${taskId}" (depth=${depth}) ${success ? 'completed' : 'failed'} in ${durationMs}ms, ${output.iterations.length} iterations`,
|
||||
);
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
@@ -210,6 +256,7 @@ export class TaskOrchestrator extends EventEmitter {
|
||||
const errMsg = (error as Error).message;
|
||||
const result: SubAgentResult = {
|
||||
taskId,
|
||||
parentSessionId: params.parentSessionId,
|
||||
result: errMsg,
|
||||
success: false,
|
||||
durationMs,
|
||||
@@ -218,7 +265,12 @@ export class TaskOrchestrator extends EventEmitter {
|
||||
|
||||
handle.status = 'error';
|
||||
handle.result = result;
|
||||
this.emit('taskError', { taskId, error: errMsg });
|
||||
this.emit('taskError', {
|
||||
taskId,
|
||||
parentSessionId: params.parentSessionId,
|
||||
description: params.description,
|
||||
error: errMsg,
|
||||
});
|
||||
|
||||
log.error(`[Orchestrator] SubAgent "${taskId}" (depth=${depth}) error: ${errMsg}`);
|
||||
|
||||
@@ -326,13 +378,25 @@ export class TaskOrchestrator extends EventEmitter {
|
||||
const handle = this.activeSubAgents.get(taskId);
|
||||
if (handle && handle.status === 'running') {
|
||||
handle.status = success ? 'completed' : 'error';
|
||||
handle.result = { taskId, result, success, durationMs: 0, iterations: 0 };
|
||||
handle.result = {
|
||||
taskId,
|
||||
parentSessionId: handle.parentSessionId,
|
||||
result,
|
||||
success,
|
||||
durationMs: 0,
|
||||
iterations: 0,
|
||||
};
|
||||
this.activeSubAgents.delete(taskId);
|
||||
this.emit('taskCompleted', handle.result);
|
||||
}
|
||||
}
|
||||
|
||||
getActiveAgentsStatus(): Array<{ taskId: string; status: string; description: string; depth: number }> {
|
||||
getActiveAgentsStatus(): Array<{
|
||||
taskId: string;
|
||||
status: string;
|
||||
description: string;
|
||||
depth: number;
|
||||
}> {
|
||||
return Array.from(this.activeSubAgents.values()).map((a) => a.getStatus());
|
||||
}
|
||||
|
||||
@@ -350,4 +414,24 @@ export class TaskOrchestrator extends EventEmitter {
|
||||
// 配合 delegate finally 块的 has() 检查:若已被 clear,finally 不再恢复(避免覆盖)。
|
||||
this.sessionDepth.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* v0.5.0: 转发 SubEngine 的事件到 orchestrator 事件总线
|
||||
*
|
||||
* SubEngine 不经过 AgentEngineManager 的转发管道(刻意隔离,避免污染父会话流),
|
||||
* 此处将其流式/状态事件重新包装为 subStreamEvent / subStateChange 广播:
|
||||
* - IPC 层监听后录制到独立 TRACE 文件(sessionId = taskId)
|
||||
* - 生命周期事件(taskDelegated 等)另行广播给前端 AgentMonitor
|
||||
*
|
||||
* @param engine SubAgent 的独立引擎实例
|
||||
* @param taskId 子任务 ID(SubEngine runStream 的 sessionId)
|
||||
*/
|
||||
private forwardSubEngineEvents(engine: AgentLoopEngine, taskId: string): void {
|
||||
engine.on('streamEvent', (event: Record<string, unknown>) => {
|
||||
this.emit('subStreamEvent', { taskId, event });
|
||||
});
|
||||
engine.on('stateChange', (data: Record<string, unknown>) => {
|
||||
this.emit('subStateChange', { taskId, data });
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user