diff --git a/README.md b/README.md index af3e8ca..d170a7a 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@

- Version + Version License Electron React @@ -54,19 +54,14 @@ --- -## 🆕 v0.8.2 更新亮点 +## 🆕 v0.8.3 更新亮点 | 特性 | 说明 | |:---|:---| -| 🛡 **适配器图片下载 SSRF 收口** | Anthropic/Ollama 的图片 URL 下载统一走 DNS Pinning 安全通道(逐跳重定向复检 + 10MB 上限 + 类型白名单),根治"诱导模型回读内网数据"的可回读外泄通道 | -| 🔐 **根 MEMORY.md 保护根治** | 闸门从工具名单枚举反转为"任意工具的路径形参数统一精确匹配"—— delete_file / file_move / MCP 文件类工具全部纳入保护 | -| 🔌 **Anthropic 协议保真** | extended thinking 块(含签名)在工具循环多轮回传;pause_turn 长回复自动续传(预算耗尽按截断语义收尾,不再静默丢失) | -| 📦 **上下文压缩正确性** | 压缩时机移至本轮消息入列后、估算纳入 System Prompt、摘要调用重试 + 纯截断兜底、故障转移后预算校正值重置 | -| 💾 **全局配置原子写 + 自愈** | global-config.json 改为 tmp+rename 原子落盘并维护备份 —— 崩溃不再丢失全部全局配置(含 LLM 凭据) | -| 🧠 **向量记忆召回修复** | 向量检索与重要度预过滤解耦(独立召回 200 条池)、embedding 模型指纹(更换模型后惰性重算自愈)、语义记忆 access_count 更新后不再归零 | -| 🎨 **工具结果富媒体渲染** | view_image / 浏览器截图在聊天流内联预览;diff_viewer / file_editor dry_run 渲染为增删着色的 unified diff 视图 | -| ⌨️ **交互断链修复** | 引用回复 / Ctrl+L 聚焦输入框(受控输入桥根治)、IPC 桥缺失兜底、根级 ErrorBoundary 逐区隔离、/export 全量导出、会话列表实时刷新、Onboarding 首帧闪烁消除 | -| 🌐 **i18n 与行为对齐** | 托盘菜单/系统对话框/剩余 8 处文案出层;MiMo 思考未配置默认关闭(尊重用户意图)、MiMo 联网搜索引用回填正文、OpenAI 推理模型 stop 参数前置拦截 | +| 🔄 **工作空间切换保存修复** | 根治"确认切换报错、重启后仍回旧空间":CSP 注入遗留模块顶层导致生产环境每次启动抛 "Session can only be received when app is ready",主进程模块求值中断,连锁令 `workspace.path` 保存链路的动态 `import('../main')` 抛 TDZ 死区错误。现安全防线(权限白名单 + CSP)统一在 ready 后、窗口创建前注册并首次真正生效,工作空间路径持久化抽离为独立模块(`utils/workspace-path.ts`)消除循环依赖;启动时再以 workspace-config.json(权威源)回写对齐当前空间 DB —— 修复继承复制滞后导致的"切换重启后设置页回显旧路径/空路径" | +| 💾 **继承数据库静默丢数修复** | `workspace:inheritFiles` 的 SQLite `backup()` 返回 Promise 未 await,源库被提前 `close()` 掐断备份 —— 目标 agent.db 仅剩空壳单页(切换后历史会话/记忆/Trace 静默丢失,重启后全量重建空表),且备份失败 rejection 无人接住(unhandledRejection)。现等待备份完成再关闭源库,失败仍回退 `copyFileSync` | +| 🧠 **思考强度新增 xhigh / true 档** | `agent.thinkingEffort` 扩展为 6 档:low / medium / high / **xhigh** / max / **true**。xhigh 原样下发 Ollama(Qwen3 等新思考模板的原生最高档,旧版只发 high 会触发模板 500 "Unexpected reasoning effort");`true` 不指定档位,交由模型思考模板用自身默认档。仅支持固定档位的 Provider 就近降档映射(OpenAI/DeepSeek → high,Anthropic → 24576/16384 token 预算) | +| 🎛 **思考参数档位单源** | `MetonaThinkingEffort` 类型收敛至 IR 层单一来源(引擎/引擎配置注入/UI 共用),消除四处内联 union 的漂移风险 | --- @@ -670,7 +665,7 @@ OLLAMA_BASE_URL=http://localhost:11434 | `agent.totalTimeoutMs` | `600000` | Agent 总超时 (ms) | | `agent.toolExecutionTimeoutMs` | `120000` | 单个工具执行超时 (ms) | | `agent.thinkingEnabled` | `true` | 启用 Thinking 推理模式 | -| `agent.thinkingEffort` | `high` | 推理强度 (low / medium / high / max) | +| `agent.thinkingEffort` | `high` | 推理强度 (low / medium / high / xhigh / max / true —— xhigh 为 Qwen3 等模板原生档,true 交由模型默认档,v0.8.3) | | `agent.confirmationTimeoutMs` | `120000` | 确认弹窗超时 (30s ~ 600s) | | `agent.enableReflection` | `false` | 反思阶段开关 — 开启后每轮工具执行经过 REFLECTING 状态(失败结果告警,不阻断) | | `memory.consolidationEnabled` | `true` | 会话结束记忆固化总开关(v0.7.3 节流策略) | diff --git a/electron/harness/adapters/__tests__/openai.adapter.test.ts b/electron/harness/adapters/__tests__/openai.adapter.test.ts index 890d030..41a1c4b 100644 --- a/electron/harness/adapters/__tests__/openai.adapter.test.ts +++ b/electron/harness/adapters/__tests__/openai.adapter.test.ts @@ -82,7 +82,9 @@ describe('OpenAIAdapter — 推理模型 reasoning_effort', () => { ['low', 'low'], ['medium', 'medium'], ['high', 'high'], + ['xhigh', 'high'], // v0.8.3: OpenAI 档位封顶 high,xhigh 就近降档 ['max', 'high'], // max 归一 high + ['true', 'high'], // v0.8.3: 模型默认档归一 high ] as const)('o3-mini effort=%s → reasoning_effort=%s', async (effort, expected) => { const adapter = makeAdapter('o3-mini'); mockFetch.mockResolvedValue(okResponse()); diff --git a/electron/harness/adapters/__tests__/provider-request-shapes.test.ts b/electron/harness/adapters/__tests__/provider-request-shapes.test.ts index dcd091e..cb7d808 100644 --- a/electron/harness/adapters/__tests__/provider-request-shapes.test.ts +++ b/electron/harness/adapters/__tests__/provider-request-shapes.test.ts @@ -13,7 +13,7 @@ * * Ollama: * O1 options 映射(num_predict=numTokens、num_ctx=contextLength、stop、top_p) - * O2 think 参数 effort 映射(low→"low"、max→true)与未配置时缺省 + * O2 think 参数 effort 映射(low→"low"、xhigh→"xhigh"、max/true→true)与未配置时缺省 * O3 图片归一化(data URI 剥前缀;无 URL 触发下载分支时零网络请求) * * Agnes: @@ -286,7 +286,7 @@ describe('OllamaAdapter — 请求体契约', () => { expect(options.stop).toEqual(['STOP']); }); - it('O2: think 参数 effort 映射(low→"low"、max→true);未开启思考时缺省', async () => { + it('O2: think 参数 effort 映射(low→"low"、xhigh→"xhigh"、max/true→true);未开启思考时缺省', async () => { const adapter = makeOllama(); const { bodies } = captureFetch(); @@ -316,12 +316,39 @@ describe('OllamaAdapter — 请求体契约', () => { ); expect(bodies[1].think).toBe(true); + // v0.8.3: xhigh 原样透传(Qwen3 等思考模板原生档);true → 布尔 true(模型默认档) + await adapter.send( + makeRequest({ + params: { + maxTokens: 4096, + temperature: 0, + stream: false, + thinkingEnabled: true, + thinkingEffort: 'xhigh', + }, + }), + ); + expect(bodies[2].think).toBe('xhigh'); + + await adapter.send( + makeRequest({ + params: { + maxTokens: 4096, + temperature: 0, + stream: false, + thinkingEnabled: true, + thinkingEffort: 'true', + }, + }), + ); + expect(bodies[3].think).toBe(true); + await adapter.send( makeRequest({ params: { maxTokens: 4096, temperature: 0, stream: false, thinkingEnabled: false }, }), ); - expect(bodies[2].think).toBeUndefined(); + expect(bodies[4].think).toBeUndefined(); }); it('O3: data URI 图片剥前缀转纯 base64 数组(无网络下载路径触发)', async () => { @@ -516,8 +543,10 @@ describe('AnthropicAdapter — thinking budget 按 effort 映射矩阵', () => { ['low', 1024], ['medium', 4096], ['high', 16384], + ['xhigh', 24576], // v0.8.3: 介于 high 与 max 之间 // v0.8.1: max_tokens 不再按模型钳制(100_000 原样透传)→ budget = min(32768, floor(100000/2)) = 32768 ['max', 32768], + ['true', 16384], // v0.8.3: 模型默认档按 high 同档预算 ] as const)('effort=%s → budget 为该档值且 < max_tokens', async (effort, expectBudget) => { const adapter = makeAdapter(); const { bodies } = captureFetch(); @@ -723,9 +752,11 @@ describe('DeepSeekAdapter — thinking 映射矩阵', () => { ['low', 'high'], ['medium', 'high'], ['high', 'high'], + ['xhigh', 'high'], // v0.8.3: DeepSeek 无 xhigh 档,就近映射 high ['max', 'max'], + ['true', 'high'], // v0.8.3: 模型默认档映射 high ] as const)( - 'effort=%s → reasoning_effort=%s(low/medium 归一 high)', + 'effort=%s → reasoning_effort=%s(low/medium/xhigh/true 归一 high)', async (effort, expected) => { const adapter = makeAdapter(); const { bodies } = captureFetch(); diff --git a/electron/harness/adapters/anthropic.adapter.ts b/electron/harness/adapters/anthropic.adapter.ts index 31a4542..af159ed 100644 --- a/electron/harness/adapters/anthropic.adapter.ts +++ b/electron/harness/adapters/anthropic.adapter.ts @@ -730,12 +730,16 @@ export class AnthropicAdapter extends BaseAdapter { } // Thinking 模式:budget_tokens(必须小于 max_tokens,此处钳制到一半) + // v0.8.3: 新增 xhigh / true 档 —— xhigh 介于 high 与 max 之间取 24576; + // true(模型默认档)按 high 同档预算 if (thinkingRequested) { const budgetMap: Record = { low: 1024, medium: 4096, high: 16384, + xhigh: 24576, max: 32768, + true: 16384, }; const effortBudget = budgetMap[request.params.thinkingEffort ?? 'high'] ?? 16384; // thinking 路径 maxTokensForRequest 恒为数字(Math.max(2048, …) 兜底) diff --git a/electron/harness/adapters/deepseek.adapter.ts b/electron/harness/adapters/deepseek.adapter.ts index 2b7c2b2..f7217d1 100644 --- a/electron/harness/adapters/deepseek.adapter.ts +++ b/electron/harness/adapters/deepseek.adapter.ts @@ -206,13 +206,16 @@ export class DeepSeekAdapter extends OpenAICompatibleAdapter { body.thinking = { type: 'disabled' }; } else { body.thinking = { type: 'enabled' }; + // v0.8.3: 新增 xhigh / true 档 —— DeepSeek API 仅 high / max 两档,就近映射 high const effortMap: Record = { low: 'high', medium: 'high', high: 'high', + xhigh: 'high', max: 'max', + true: 'high', }; - // DeepSeek API 仅支持 high / max 两档,low/medium 映射为 high + // DeepSeek API 仅支持 high / max 两档,low/medium/xhigh/true 映射为 high body.reasoning_effort = effortMap[request.params.thinkingEffort ?? 'high'] ?? 'high'; // 元信息标注不支持思考但用户开启 —— 告知降级兜底路径(不拦截) if (DeepSeekAdapter.MODEL_INFO[this.config.defaultModel]?.supportsThinking === false) { diff --git a/electron/harness/adapters/ollama.adapter.ts b/electron/harness/adapters/ollama.adapter.ts index da49236..8e3882b 100644 --- a/electron/harness/adapters/ollama.adapter.ts +++ b/electron/harness/adapters/ollama.adapter.ts @@ -698,11 +698,17 @@ export class OllamaAdapter extends BaseAdapter { if (request.params.thinkingEnabled) { const modelThinkingSupported = this.cachedThinkingSupport !== false; if (modelThinkingSupported) { + // v0.8.3: 新增 xhigh / true 档 —— Qwen3 等新模型思考模板原生支持 xhigh + //(旧版仅下发 low/medium/high 时,模板会 500 "Unexpected reasoning effort"); + // true = 不指定档位(think: true),由模型思考模板用自身默认档。 + // max 语义为"应用内最高档",Ollama 侧同样以布尔 true 放行给服务端默认。 const effortMap: Record = { low: 'low', medium: 'medium', high: 'high', + xhigh: 'xhigh', max: true, + true: true, }; body.think = effortMap[request.params.thinkingEffort ?? 'high'] ?? true; // v0.8.0 P0-3: 思考占用 num_predict 输出预算 —— 预算过小时显式告警 diff --git a/electron/harness/adapters/openai.adapter.ts b/electron/harness/adapters/openai.adapter.ts index abf69f3..728763e 100644 --- a/electron/harness/adapters/openai.adapter.ts +++ b/electron/harness/adapters/openai.adapter.ts @@ -149,12 +149,15 @@ export class OpenAIAdapter extends OpenAICompatibleAdapter { } // Thinking 模式:推理模型映射 reasoning_effort;非推理模型忽略 + // v0.8.3: 新增 xhigh / true 档 —— OpenAI API 档位封顶 high,二者就近映射 if (request.params.thinkingEnabled && isReasoningModel) { const effortMap: Record = { low: 'low', medium: 'medium', high: 'high', + xhigh: 'high', max: 'high', + true: 'high', }; body.reasoning_effort = effortMap[request.params.thinkingEffort ?? 'high'] ?? 'high'; // v0.8.0 P0-3: 推理 token 计入 max_completion_tokens —— 用户配置的预算过小时告警 diff --git a/electron/harness/agent-loop/types.ts b/electron/harness/agent-loop/types.ts index bb5e801..11af0d3 100644 --- a/electron/harness/agent-loop/types.ts +++ b/electron/harness/agent-loop/types.ts @@ -4,7 +4,12 @@ * 用于 Agent Loop 引擎内部的状态管理和迭代记录。 */ -import type { MetonaToolCall, MetonaToolResult, MetonaThinkingBlock } from '../types'; +import type { + MetonaThinkingEffort, + MetonaToolCall, + MetonaToolResult, + MetonaThinkingBlock, +} from '../types'; // ===== Agent Loop 状态机 ===== @@ -100,8 +105,8 @@ export interface AgentLoopConfig { maxTokens?: number; /** 是否启用思考模式(默认 true) */ thinkingEnabled: boolean; - /** 思考强度(默认 'high') */ - thinkingEffort: 'low' | 'medium' | 'high' | 'max'; + /** 思考强度(默认 'high';v0.8.3 档位单源见 MetonaThinkingEffort) */ + thinkingEffort: MetonaThinkingEffort; /** Ollama num_ctx(与「上下文长度」同源:llm.contextWindow,仅 Ollama Provider 下发) */ contextLength?: number; /** 工具执行兜底超时(ms,默认 120000),实际取 max(此值, tool.timeoutMs) */ diff --git a/electron/harness/types/index.ts b/electron/harness/types/index.ts index 9241efc..1c3fb09 100644 --- a/electron/harness/types/index.ts +++ b/electron/harness/types/index.ts @@ -11,6 +11,7 @@ export type { MetonaRequestMeta, MetonaSystemPrompt, MetonaGenerationParams, + MetonaThinkingEffort, MetonaConstraints, MetonaMessage, MetonaImageContent, diff --git a/electron/harness/types/metona-request.ts b/electron/harness/types/metona-request.ts index 079e348..0bd7799 100644 --- a/electron/harness/types/metona-request.ts +++ b/electron/harness/types/metona-request.ts @@ -38,6 +38,17 @@ export interface MetonaSystemPrompt { // ===== 生成参数 ===== +/** + * 思考强度档位(v0.8.3 起单一类型源,引擎/引擎配置/UI/各 Adapter 共用) + * + * - low / medium / high / max:原始四档 + * - xhigh:v0.8.3 新增 —— Qwen3 等新模型思考模板原生支持 xhigh 档 + * (Ollama 下原样下发;仅支持 low/medium/high 的 Provider 就近降档映射) + * - true:v0.8.3 新增 —— 不指定档位,`think: true` 交给模型思考模板用自身默认 + * 档(如 Qwen3 默认 xhigh);有显式档位协议的 Provider 就近映射为 high + */ +export type MetonaThinkingEffort = 'low' | 'medium' | 'high' | 'xhigh' | 'max' | 'true'; + export interface MetonaGenerationParams { /** 最大生成 token 数 */ maxTokens?: number; @@ -52,7 +63,7 @@ export interface MetonaGenerationParams { /** 是否启用思考模式 */ thinkingEnabled?: boolean; /** 思考强度(替代 thinkingBudget,各 Provider 映射见 Adapter 规范) */ - thinkingEffort?: 'low' | 'medium' | 'high' | 'max'; + thinkingEffort?: MetonaThinkingEffort; /** 上下文窗口大小(仅 Ollama 支持 num_ctx) */ contextLength?: number; } diff --git a/electron/ipc/shared.ts b/electron/ipc/shared.ts index ee68134..0dfe1f6 100644 --- a/electron/ipc/shared.ts +++ b/electron/ipc/shared.ts @@ -14,6 +14,8 @@ import { isSensitiveConfigKey } from '../utils/secure-config'; import { maskSensitiveValue } from '../utils/mask'; // v0.8.1 P2-1: 工具自定义策略解析 import { parseToolPolicy } from '../harness/sandbox/permissions'; +// v0.8.3: 思考强度档位单源(新增 xhigh / true 档) +import type { MetonaThinkingEffort } from '../harness/types'; // v0.8.0 P1-5: 配置 URL 深校验(域名真实 DNS 解析,拦"解析到云元数据 IP"绕过) import { assertSafeConfigTargetDeep, @@ -21,6 +23,10 @@ import { } from '../harness/tools/built-in/ssrf-guard'; // v0.8.1 P0-4: 主进程文案双语 import { setMainLocale, mt } from '../utils/main-locale'; +// v0.8.3: 工作空间路径持久化独立成模块 —— 此前动态 import('../main') 与入口模块 +// 形成循环依赖,入口求值一旦中断即抛 TDZ "Cannot access 'main' before +// initialization",工作空间切换保存随之失败 +import { writeWorkspacePathToFile } from '../utils/workspace-path'; /** * v0.7.4 P2-9-C: URL 类配置键 —— 写入时须过 assertSafeConfigTarget 高危目标校验。 @@ -149,10 +155,10 @@ export function applyEngineConfigKey(ctx: IPCContext, key: string, value: unknow break; case 'agent.thinkingEffort': agentEngineManager.updateConfigAll({ - thinkingEffort: value as 'low' | 'medium' | 'high' | 'max', + thinkingEffort: value as MetonaThinkingEffort, }); orchestrator.updateDefaultConfig({ - thinkingEffort: value as 'low' | 'medium' | 'high' | 'max', + thinkingEffort: value as MetonaThinkingEffort, }); break; // v0.7.3 P3-1: enableReflection 接线(此前为死配置)— 引擎 REFLECTING 状态开关 @@ -293,7 +299,6 @@ export async function applyConfigSideEffects( ); if (workspaceEntry) { try { - const { writeWorkspacePathToFile } = await import('../main'); writeWorkspacePathToFile(workspaceEntry.value as string); log.info(`[CONFIG] Workspace path saved (restart required): ${workspaceEntry.value}`); } catch (err) { diff --git a/electron/ipc/workspace.ts b/electron/ipc/workspace.ts index f1f2810..8230638 100644 --- a/electron/ipc/workspace.ts +++ b/electron/ipc/workspace.ts @@ -161,7 +161,10 @@ export function registerWorkspaceHandlers(ctx: IPCContext): void { const Database = (await import('better-sqlite3')).default; const srcDb = new Database(srcFile, { readonly: true, fileMustExist: true }); try { - srcDb.backup(dstFile); + // v0.8.3 修复: backup() 返回 Promise,必须 await —— 原实现未等待就 + // close() 源库,备份被中途掐断:目标库只剩空壳页(继承数据静默丢失, + // 重启后全量重建表),且 rejection 无人接住(unhandledRejection FATAL)。 + await srcDb.backup(dstFile); inherited.push(fileName); log.info( `[WORKSPACE] Inherited ${fileName} (via SQLite backup): ${resolvedSource} → ${resolvedTarget}`, diff --git a/electron/main.ts b/electron/main.ts index 0bc7c7f..2399414 100644 --- a/electron/main.ts +++ b/electron/main.ts @@ -17,7 +17,6 @@ import 'dotenv/config'; import { app, shell, Menu, BrowserWindow, dialog, session } from 'electron'; import { join } from 'path'; -import { existsSync, readFileSync, writeFileSync } from 'fs'; import { electronApp, optimizer } from '@electron-toolkit/utils'; import log from 'electron-log'; import { DatabaseService } from './services/database.service'; @@ -47,6 +46,8 @@ import { OllamaAdapter } from './harness/adapters/ollama.adapter'; import { OpenAIAdapter } from './harness/adapters/openai.adapter'; import { AnthropicAdapter } from './harness/adapters/anthropic.adapter'; import type { IMetonaProviderAdapter } from './harness/types/metona-adapter'; +// v0.8.3: 思考强度档位单源(新增 xhigh / true 档) +import type { MetonaThinkingEffort } from './harness/types'; import { ReadFileTool, WriteFileTool, @@ -95,6 +96,10 @@ import { HealthChecker, SLOMonitor } from './utils/slo'; // v0.8.1 P0-4: 主进程 toast/通知文案双语(ui.locale 驱动) import { setMainLocale, mt } from './utils/main-locale'; // v0.6.4 P4-5: session 级网络代理应用工具(default + agent-browser 分区) +// v0.8.3: 工作空间路径持久化独立成模块(消除 main ↔ ipc/shared 循环依赖 —— +// 此前 shared.ts 动态 import('../main') 取 writeWorkspacePathToFile,入口模块 +// 求值一旦中断即抛 TDZ,工作空间切换保存随之报错) +import { readWorkspacePathFromFile, writeWorkspacePathToFile } from './utils/workspace-path'; // ===== 步骤 1: 初始化日志系统(SYS 层)===== log.transports.file.level = 'info'; @@ -130,31 +135,6 @@ if (process.env['METONA_USER_DATA_DIR']) { app.setPath('userData', process.env['METONA_USER_DATA_DIR']); } -// ===== 工作空间路径独立存储(解决 DB 在 workspace 内的鸡生蛋问题)===== -const WORKSPACE_CONFIG_FILE = join(app.getPath('userData'), 'workspace-config.json'); - -function readWorkspacePathFromFile(): string | null { - try { - if (existsSync(WORKSPACE_CONFIG_FILE)) { - const data = JSON.parse(readFileSync(WORKSPACE_CONFIG_FILE, 'utf-8')); - return data.workspacePath ?? null; - } - } catch { - // 忽略读取错误 - } - return null; -} - -function writeWorkspacePathToFile(workspacePath: string): void { - try { - writeFileSync(WORKSPACE_CONFIG_FILE, JSON.stringify({ workspacePath }, null, 2), 'utf-8'); - } catch (err) { - log.error('Failed to write workspace config file:', err); - } -} - -export { readWorkspacePathFromFile, writeWorkspacePathToFile }; - let databaseService: DatabaseService | null = null; let trayManager: TrayManager | null = null; let windowManager: WindowManager | null = null; @@ -196,8 +176,14 @@ async function initialize(): Promise { const workspaceService = new WorkspaceService(savedWorkspacePath ?? undefined); const workspaceInfo = workspaceService.initialize(); // 持久化工作空间路径(供下次启动读取) + // v0.8.3: writeWorkspacePathToFile 改为向上抛错(供切换工作空间场景 toast 告知), + // 启动期回写失败非致命 —— 仅日志留痕,不中断初始化。 if (savedWorkspacePath !== workspaceInfo.path) { - writeWorkspacePathToFile(workspaceInfo.path); + try { + writeWorkspacePathToFile(workspaceInfo.path); + } catch { + // 错误已在 writeWorkspacePathToFile 内记录 + } } log.info( `Workspace: ${workspaceInfo.path} (missing: ${workspaceInfo.missingFiles.join(', ') || 'none'})`, @@ -220,6 +206,19 @@ async function initialize(): Promise { // v0.8.1 P0-4: 主进程语言随 ui.locale 注入(变更经 applyConfigSideEffects 热切换) setMainLocale(configService.get('ui.locale')); + // v0.8.3: workspace.path 权威源对齐(设置页回显修复) + // 切换工作空间时,新路径经 config:set 写入的是旧空间的 DB,而"继承数据库"复制 + // 发生在该写入之前 —— 新空间的 DB 携带的是滞后一位的旧路径;未继承 DB 的全新 + // 空间则根本没有该键。重启后设置页从当前空间 DB 读回显,显示的便不是当前空间。 + // workspace-config.json 是启动权威源(readWorkspacePathFromFile 决定本次加载哪个 + // 空间),这里把它回写进当前空间 DB,自愈一切来源的漂移(继承复制滞后 / 全新 + // 空间缺键 / 引导向导写入),保证 config:get('workspace.path') 与实际加载的空间 + // 永远一致。 + if (configService.get('workspace.path') !== workspaceInfo.path) { + configService.set('workspace.path', workspaceInfo.path); + log.info(`[CONFIG] workspace.path synced to loaded workspace: ${workspaceInfo.path}`); + } + // v0.8.1 P2-5: E2E 引导种子(见文件顶部说明;仅 METONA_E2E_SEED_CONFIG 时生效) if (process.env['METONA_E2E_SEED_CONFIG'] === '1') { configService.set('onboarding.completed', true); @@ -476,12 +475,8 @@ async function initialize(): Promise { contextWindow: getContextWindowSetting(), thinkingEnabled: configService.get('agent.enableThinking') ?? true, thinkingEffort: - (configService.get('agent.thinkingEffort') as - | 'low' - | 'medium' - | 'high' - | 'max' - | null) ?? 'high', + (configService.get('agent.thinkingEffort') as MetonaThinkingEffort | null) ?? + 'high', toolExecutionTimeoutMs: configService.get('agent.toolExecutionTimeoutMs') ?? 120_000, // v0.7.3 P3-1 接线: agent.enableReflection 此前为死配置(引擎读取但全链路 // 无置 true 路径)。现注入引擎基线配置,REFLECTING 状态由此开关真实驱动。 @@ -627,12 +622,8 @@ async function initialize(): Promise { { thinkingEnabled: configService.get('agent.enableThinking') ?? true, thinkingEffort: - (configService.get('agent.thinkingEffort') as - | 'low' - | 'medium' - | 'high' - | 'max' - | null) ?? 'high', + (configService.get('agent.thinkingEffort') as MetonaThinkingEffort | null) ?? + 'high', contextLength: getOllamaContextLength(), contextWindow: getContextWindowSetting(), // v0.7.3 P3-1: SubAgent 与主引擎同源消费 enableReflection @@ -1048,8 +1039,11 @@ if (!gotSingleInstanceLock) { // v0.5.0: 启动链路异常兜底 — DB 损坏/工作空间不可写等初始化失败时, // 原实现会静默挂起(渲染进程白屏且无任何用户可见错误)。 // 兜底策略:记录日志 + 弹出系统错误对话框 + 退出(exit 1) + // v0.8.3: 安全防线在同一 ready 链上先于 initialize 注册(回调按挂载顺序执行) + // —— CSP 拦截必须在主窗口发起首个 mainFrame 请求前就位,否则首页响应漏注入。 app .whenReady() + .then(registerSecurityHardening) .then(initialize) .catch((err) => { log.error('[Startup] Initialization failed:', err); @@ -1072,57 +1066,68 @@ if (!gotSingleInstanceLock) { // 此前主窗口无 CSP、无 permission handler —— notifications/geo/media/clipboard // 等请求全部走 Chromium 默认放行,且渲染层一旦被注入可静默触达敏感能力。 // 两条防线均为 deny-by-default 白名单制,任何一条失败都不放大攻击面。 -{ +// +// v0.8.1 根治: session.defaultSession 必须在 app ready 后才能访问 —— 此前在 +// 模块加载期(ready 前)直接调用,每次启动都抛 "Session can only be received +// when app is ready",权限白名单静默失效(FATAL 日志为证)。防线一延迟到 ready +// 后注册,防线真正生效。 +// +// v0.8.3 根治收尾: 防线二(CSP)当时漏改,仍留在模块顶层 —— 生产环境每次启动 +// 都在此抛 "Session can only be received when app is ready",模块求值中止: +// 权限白名单与 CSP 实际从未注册,且打包产物末尾的模块命名空间永不初始化, +// 连锁导致 ipc/shared.ts 保存 workspace.path 的动态 import 抛 TDZ +// (工作空间切换保存失败,utils/workspace-path.ts 头注有完整因果链)。 +// 现两条防线统一收敛为 registerSecurityHardening,挂载在 initialize 之前执行; +// 单防线失败仅记日志,不阻断启动链。 +function registerSecurityHardening(): void { // 防线一:权限请求白名单(deny-by-default) - // v0.8.1 根治: session.defaultSession 必须在 app ready 后才能访问 —— 此前在 - // 模块加载期(ready 前)直接调用,每次启动都抛 "Session can only be received - // when app is ready",权限白名单静默失效(FATAL 日志为证)。现延迟到 ready 后 - // 注册,防线真正生效。 - const ALLOWED_PERMISSIONS = new Set(['clipboard-sanitized-write', 'fullscreen']); - void app - .whenReady() - .then(() => { - session.defaultSession.setPermissionRequestHandler((_wc, permission, callback) => { - callback(ALLOWED_PERMISSIONS.has(permission)); - }); - session.defaultSession.setPermissionCheckHandler((_wc, permission) => - ALLOWED_PERMISSIONS.has(permission), - ); - log.info('[Security] Permission whitelist registered (app ready)'); - }) - .catch((err) => { - log.error('[Security] Failed to register permission whitelist:', err); + try { + const ALLOWED_PERMISSIONS = new Set(['clipboard-sanitized-write', 'fullscreen']); + session.defaultSession.setPermissionRequestHandler((_wc, permission, callback) => { + callback(ALLOWED_PERMISSIONS.has(permission)); }); + session.defaultSession.setPermissionCheckHandler((_wc, permission) => + ALLOWED_PERMISSIONS.has(permission), + ); + log.info('[Security] Permission whitelist registered (app ready)'); + } catch (err) { + log.error('[Security] Failed to register permission whitelist:', err); + } // 防线二:生产环境 CSP 注入(仅 mainFrame,不触碰 dev server 的 HMR)。 // MUI/emotion 需要 style-src 'unsafe-inline'(运行时注入