feat: v0.7.2 安全收口 · 断链接线 · 观测补洞 — 230 用例扩充与全量回归
P1 修复面收口: /clear 全链路根治(前端清空联动 DB messages+摘要游标+TRACE 快照, IPC 语义改"操作完成"; 流式中拒绝); web_browser open 补 SSRF 校验(Chromium 旁路关闭, 与 web_fetch/http_request 同源 validateSSRF); MCP 工具结果纳入注入扫描(mcp_* 前缀 按网络来源同级 full 模式, 收敛 resolveScanMode 单点); Trace 落库/入 store 双重瘦身 (tool_result base64/超长字段剥离, metadata 防 MB 级膨胀); 文本附件 512KB 闸门 (file.slice 首段读取+truncated 标志随消息持久化+主进程附件提示感知截断); 单实例锁(requestSingleInstanceLock + second-instance 聚焦已有窗口) P2 安全纵深: ConfirmationHook 多窗口化(确认请求/超时提示改全窗口广播, getAllWindows 空时回退 mainWindow, fail-closed 判定升级双通道); mcp_servers.headers 全链路接线(safeParseHeaders 容错解析+SSE/StreamableHTTP requestInit 注入+IPC 逐项 校验+设置页 JSON 输入, 远程 MCP 鉴权头可用) P3 断链接线: llm:listModels IPC(六家 adapter 动态模型发现首次接线, 配置完整性 前置校验); Ollama pullModel IPC+设置页下载卡片(进度/取消/能力徽标, v0.7.0 死代码 激活); 后台会话运行指示(sessionRunStates 图+Sidebar 状态点, 多会话并发可见); IR 卫生(移除 THINKING_START/END 死枚举, constraints 标注预留) P4 质量与文档: i18n 第二阶段(确认弹框/侧栏/状态栏/AgentMonitor/终止原因出层, 外观设置 zh-CN/en-US 切换, ui.locale 持久化, 渲染时求值规避异步注册); README/D1 文档对齐(http_request 风险等级/用例数/实现状态注记); 版本号 0.7.2 测试: 507 → 737 用例(+230, 11 个新文件)。覆盖补齐: context-builder/consolidator/ orchestrator/workspace.service/session-recorder/config-layering/secure-config/ network-proxy + IPC mcp/tasks/memory/app/data 域 + 渲染层 store 与流事件管线纯函数。 测试驱动修复: workspace.appendMemory 中文分区 \b 词边界失效(JS \b 不含 CJK), 固化条目恒追加文件末尾产生重复分区头 → (?=\n|$) 前瞻断言根治 回归: typecheck 双端 0 错误; ESLint 0/0; 系统 Node 687 通过 50 跳过; Electron ABI 全量 737/737 零跳过
This commit is contained in:
+80
-79
@@ -20,6 +20,29 @@ import {
|
||||
// v0.6.4 P3-5: 文案集中字典(含注册副作用,须在 t() 使用前 import)
|
||||
import { t } from '@renderer/lib/i18n';
|
||||
import '@renderer/lib/i18n-strings';
|
||||
// v0.7.2 P4-13: 事件处理判定抽取为纯函数(表测锁定关键回归路径)
|
||||
import {
|
||||
decideStreamEvent,
|
||||
decideStateEvent,
|
||||
needsNewAssistantCard,
|
||||
findTraceStepIndexForToolCall,
|
||||
} from '@renderer/lib/stream-event-pipeline';
|
||||
// v0.7.2 A4: traceSteps 入 store 时即剥离工具结果中的 base64/超长字段
|
||||
import { toDisplayResult } from '@renderer/lib/tool-result-display';
|
||||
|
||||
/**
|
||||
* v0.7.2 P3-11: 后台会话运行状态映射(stateChange → sessionRunStates 指示图)。
|
||||
* 与下方 stateToStatus 的差异:INIT 不映射(无实际内容);
|
||||
* TERMINATED 不在此映射(调用方直接移除条目)。
|
||||
*/
|
||||
const BACKGROUND_RUN_STATUS: Record<string, AgentStatus> = {
|
||||
THINKING: 'thinking',
|
||||
PARSING: 'thinking',
|
||||
OBSERVING: 'thinking',
|
||||
REFLECTING: 'thinking',
|
||||
COMPRESSING: 'thinking',
|
||||
EXECUTING: 'executing',
|
||||
};
|
||||
|
||||
/**
|
||||
* Agent 流式事件监听 Hook
|
||||
@@ -143,35 +166,16 @@ export function useAgentStream(): void {
|
||||
state?: string;
|
||||
};
|
||||
|
||||
// 会话过滤:忽略非当前会话的事件(防止切换会话后旧事件污染)
|
||||
if (data.sessionId && data.sessionId !== useAgentStore.getState().currentSessionId) return;
|
||||
|
||||
// H-6/C-4: runId 过滤 — 忽略旧 run 的事件(abort 后重发场景)
|
||||
// 修复 abort 后旧 DONE 污染新 run:currentRunId=null 时忽略终止事件
|
||||
const currentState = useAgentStore.getState();
|
||||
if (data.runId) {
|
||||
if (currentState.currentRunId) {
|
||||
// currentRunId 已设置:只接受匹配的事件
|
||||
if (data.runId !== currentState.currentRunId) return;
|
||||
} else {
|
||||
// currentRunId 未设置:
|
||||
// done/error 是终止事件 — currentRunId 为 null 说明已被 abort 或尚未开始,
|
||||
// 忽略旧 run 的延迟终止事件,避免错误地 setStreaming(false)
|
||||
if (data.type === 'done' || data.type === 'error') return;
|
||||
// 首个活跃事件,设置 currentRunId
|
||||
currentState.setCurrentRunId(data.runId);
|
||||
}
|
||||
}
|
||||
|
||||
// v0.6.4: abort 尾巴过滤 —— 用户中断后 agent-store 已置 idle(isStreaming=false)
|
||||
// 并刻意保留 currentRunId 用于吞掉延迟终止事件;但引擎在 abort 生效的间隙里
|
||||
// 仍会放出同 runId 的尾部增量(reasoning/text delta、TOOL_RESULT 等)。
|
||||
// 此前这些事件照常写入 messages —— 停止按钮按下后聊天区还会"自己长出内容"。
|
||||
// 规则:非流式状态下除终止事件(done/error,负责收尾落盘与解锁等待方)外,
|
||||
// 其余内容类事件一律丢弃。
|
||||
if (!currentState.isStreaming && data.type !== 'done' && data.type !== 'error') {
|
||||
return;
|
||||
}
|
||||
// ===== 事件准入判定(v0.7.2 P4-13: 纯函数抽取)=====
|
||||
// 覆盖:跨会话拒绝 / runId 守卫(含旧 run 终止事件吞掉)/ abort 尾巴过滤
|
||||
const storeBefore = useAgentStore.getState();
|
||||
const decision = decideStreamEvent(data, {
|
||||
currentSessionId: storeBefore.currentSessionId,
|
||||
currentRunId: storeBefore.currentRunId,
|
||||
isStreaming: storeBefore.isStreaming,
|
||||
});
|
||||
if (decision.action === 'reject') return;
|
||||
if (decision.adoptRunId) storeBefore.setCurrentRunId(decision.adoptRunId);
|
||||
|
||||
// 每次都从 store 读取最新状态(避免闭包捕获过期快照)
|
||||
const getStore = () => useAgentStore.getState();
|
||||
@@ -184,11 +188,8 @@ export function useAgentStream(): void {
|
||||
if (data.iteration != null) {
|
||||
const msgs = getStore().messages;
|
||||
const last = msgs[msgs.length - 1];
|
||||
const needsNewCard =
|
||||
!last ||
|
||||
last.role !== 'assistant' ||
|
||||
(last.iteration != null && last.iteration !== data.iteration);
|
||||
if (needsNewCard) {
|
||||
// v0.7.2 P4-13: 卡片边界判定抽取为纯函数
|
||||
if (needsNewAssistantCard(last, data.iteration)) {
|
||||
getStore().addMessage({
|
||||
id: genMsgId('assistant'),
|
||||
role: 'assistant',
|
||||
@@ -244,11 +245,8 @@ export function useAgentStream(): void {
|
||||
if (data.iteration != null) {
|
||||
const msgs = getStore().messages;
|
||||
const last = msgs[msgs.length - 1];
|
||||
const needsNewCard =
|
||||
!last ||
|
||||
last.role !== 'assistant' ||
|
||||
(last.iteration != null && last.iteration !== data.iteration);
|
||||
if (needsNewCard) {
|
||||
// v0.7.2 P4-13: 卡片边界判定抽取为纯函数
|
||||
if (needsNewAssistantCard(last, data.iteration)) {
|
||||
// F5 + v0.6.4: 新迭代前先 flush 全部旧缓冲区
|
||||
// (text delta 属于上一条消息;reasoning 尾巴属于上一迭代的 step)
|
||||
if (textRafIdRef.current !== null) {
|
||||
@@ -365,29 +363,27 @@ export function useAgentStream(): void {
|
||||
}
|
||||
|
||||
// 反向搜索包含该 toolCallId 的 TraceStep(修复工具结果丢失)
|
||||
// tool_call_complete 在 THINKING 阶段写入,tool_result 在 EXECUTING 阶段到达
|
||||
// 两者可能在同一轮迭代但不同状态转换,需反向查找
|
||||
// v0.7.2 P4-13: 反向归属查找抽取为纯函数;v0.7.2 A4: 写入 traceSteps 的
|
||||
// result 先行瘦身(剥离 base64/超长字段)—— messages 保留原始结果
|
||||
//(ToolResultBlock 渲染时自行裁剪),trace 侧无任何消费方需要原始大字段
|
||||
const steps = getStore().traceSteps;
|
||||
for (let i = steps.length - 1; i >= 0; i--) {
|
||||
const trace = steps[i];
|
||||
if (trace.toolCalls?.some((tc) => tc.id === data.toolResult!.toolCallId)) {
|
||||
const updatedTraceToolCalls = trace.toolCalls.map((tc) =>
|
||||
tc.id === data.toolResult!.toolCallId
|
||||
? {
|
||||
...tc,
|
||||
status: data.toolResult!.success
|
||||
? ('success' as const)
|
||||
: ('error' as const),
|
||||
result: data.toolResult!.result,
|
||||
error: data.toolResult!.error,
|
||||
durationMs: data.toolResult!.durationMs,
|
||||
}
|
||||
: tc,
|
||||
);
|
||||
// L-2: 按 ID 精确匹配 traceStep(避免 iteration 碰撞)
|
||||
getStore().updateTraceStepById(trace.id, { toolCalls: updatedTraceToolCalls });
|
||||
break;
|
||||
}
|
||||
const traceIdx = findTraceStepIndexForToolCall(steps, data.toolResult.toolCallId);
|
||||
if (traceIdx >= 0) {
|
||||
const trace = steps[traceIdx];
|
||||
// findTraceStepIndexForToolCall 命中即保证 toolCalls 非空(?? [] 为类型收窄兜底)
|
||||
const updatedTraceToolCalls = (trace.toolCalls ?? []).map((tc) =>
|
||||
tc.id === data.toolResult!.toolCallId
|
||||
? {
|
||||
...tc,
|
||||
status: data.toolResult!.success ? ('success' as const) : ('error' as const),
|
||||
result: toDisplayResult(data.toolResult!.result),
|
||||
error: data.toolResult!.error,
|
||||
durationMs: data.toolResult!.durationMs,
|
||||
}
|
||||
: tc,
|
||||
);
|
||||
// L-2: 按 ID 精确匹配 traceStep(避免 iteration 碰撞)
|
||||
getStore().updateTraceStepById(trace.id, { toolCalls: updatedTraceToolCalls });
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -458,7 +454,8 @@ export function useAgentStream(): void {
|
||||
const reasonLabels: Record<string, string> = {
|
||||
max_iterations: t('agent.terminated.max_iterations'),
|
||||
timeout: t('agent.terminated.timeout'),
|
||||
error: '执行出错',
|
||||
// v0.7.2 P4-15: 此前硬编码的中文文案出层
|
||||
error: t('agent.terminated.error'),
|
||||
};
|
||||
getStore().addMessage({
|
||||
id: genMsgId('system'),
|
||||
@@ -560,27 +557,31 @@ export function useAgentStream(): void {
|
||||
|
||||
const store = useAgentStore.getState();
|
||||
|
||||
// 会话过滤:忽略非当前会话的状态变化
|
||||
if (data.sessionId && data.sessionId !== store.currentSessionId) return;
|
||||
|
||||
// H-6/C-4: runId 过滤 — 忽略旧 run 的状态变化
|
||||
// 修复 abort 后旧 TERMINATED 污染:currentRunId=null 时只有 INIT 设置 currentRunId,其他忽略
|
||||
if (data.runId) {
|
||||
if (store.currentRunId) {
|
||||
// currentRunId 已设置:只接受匹配的状态变化
|
||||
if (data.runId !== store.currentRunId) return;
|
||||
// ===== v0.7.2 P3-11: 后台会话运行指示 =====
|
||||
// 非当前会话的 stateChange 不进入消息流/Trace(多会话隔离不变),
|
||||
// 但必须维护 sessionRunStates 指示图 —— 用户切走会话后该会话的 Agent
|
||||
// 仍在运行,Sidebar 需要点亮其状态点。TERMINATED 移除条目,运行结束。
|
||||
if (data.sessionId && data.sessionId !== store.currentSessionId) {
|
||||
const st = data.state ?? data.current ?? '';
|
||||
if (st === 'TERMINATED') {
|
||||
store.updateSessionRunState(data.sessionId, null);
|
||||
} else {
|
||||
// currentRunId 未设置:
|
||||
// 只有 INIT 是新 run 的第一个状态,设置 currentRunId;
|
||||
// 其他状态(如旧 run 的 TERMINATED)忽略,避免污染
|
||||
if (data.state === 'INIT') {
|
||||
store.setCurrentRunId(data.runId);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
const mapped = BACKGROUND_RUN_STATUS[st];
|
||||
if (mapped) store.updateSessionRunState(data.sessionId, mapped);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// ===== 事件准入判定(v0.7.2 P4-13: 纯函数抽取)=====
|
||||
// 覆盖:跨会话拒绝 / runId 守卫(currentRunId 未设置时仅 INIT 采纳,
|
||||
// 其余为旧 run 的延迟状态变化 —— abort 后重发场景的污染源)
|
||||
const decision = decideStateEvent(data, {
|
||||
currentSessionId: store.currentSessionId,
|
||||
currentRunId: store.currentRunId,
|
||||
});
|
||||
if (decision.action === 'reject') return;
|
||||
if (decision.adoptRunId) store.setCurrentRunId(decision.adoptRunId);
|
||||
|
||||
// --- 迭代号更新 + 新消息卡片创建 ---
|
||||
if (data.iteration != null && data.iteration !== store.currentIteration) {
|
||||
const prevIteration = store.currentIteration;
|
||||
|
||||
Reference in New Issue
Block a user