Files
metona-ai-desktop/electron/services/workspace.service.ts
T
thzxx 5b9d4d19b3
CI / 类型检查 + Lint + 单元测试 (push) Failing after 9m16s
CI / 全量测试 (Electron ABI) (push) Failing after 6m4s
CI / 产物编译验证 (push) Successful in 11m1s
feat: v0.8.0 流语义补全 · 会话可靠 · 恢复力 — finish_reason 全链路贯通根治"思考中停止" · 2445 用例全量回归
P0 会话可靠性收口(根治"模型思考着会话就停止"):
- P0-1 finish_reason 全链路贯通:DONE 事件与 IterationStep 新增 finishReason,OpenAI 共享 SSE / Anthropic message_delta.stop_reason / Ollama done_reason 三路采集,TRACE 层弃用硬编码 'stop' 记录真值
- P0-2 空响应守卫 + 降级重试:零产出流→可重试错误走退避;思考耗尽输出预算(reasoning-only + length)→自动关闭思考降级重试一次;仍失败→OUTPUT_LENGTH_EXCEEDED 结构化错误 + 故障转移;附带根治 abort 恰逢零工具调用轮被 COMPLETED 抢占的真实缺陷
- P0-3 思考×能力×预算三对齐:DeepSeek/MiMo/Agnes/Ollama 四家 supportsThinking=false 强制不发思考参数;小输出预算告警;设置页联动提示
- P0-4 渲染层可见性:截断/空完成/友好错误三类提示,i18n 全部出层
- P0-5 回归四件套:reasoning-only 终止判定、集成级空闲超时、504 引擎重试归类、思考中 abort→USER_INTERRUPT、P4-2 强制收尾路径

FEAT-1:LLM 设置新增「最大输出上限」——Provider 支持矩阵显隐 + 模型上限钳制提示 + 超限保存警告 + llm.maxTokens 热生效

P1 修复面收口:
- 渲染层三缺陷根治:后台会话回放缓冲(2000 条/4MB 有界 + agent:getReplayState + 事件总线)+ abort 双层自愈 + sendMessage 收尾兜底 + 中断卡片清扫
- 工具 abort 信号全覆盖:web_search/web_fetch/http_request/code_search/git 系列/delegate_task 全部接入引擎中断;web_search 时间预算收敛(720s→≤240s);移除伪造 ToolExecutionContext 与死代码
- 安全:本地 Pinned CONNECT 代理根治浏览器通道 DNS rebinding(校验期 IP pinning,可注入 resolver 表测);配置 URL 域名解析深校验(DeepCheckSoftFailure 软失败);SSE 空 error 帧防御修复;Ollama generate/embed AbortSignal.any 合并
- 缺陷清单:UTF-16 BOM 读取、tmp 同毫秒碰撞(nanoid 后缀)、code_search JS 回退参数对称(case_sensitive/前后文独立)、list_directory include_node_modules、崩溃自愈退避(60s 窗 ≥3 次停 reload)、MemoryViewer/Sidebar i18n 收口

P2 能力演进:
- 会话回收站:SCHEMA_VERSION 3 + 迁移 10(deleted_at,存在性守卫),软删除/恢复/彻底删除/30 天自动清理(启动+24h),searchMessages 聚合剔除,Sidebar 回收站面板
- 会话回放播放器:sessions:listRecordings/readRecording(白名单+目录边界+20MB 上限),SessionReplayPlayer 时间轴/步进/变速,Trace 面板入口
- electron-updater 自动更新:双轨(手动 feed 比对保留),生产环境启动静默检查 + update:status 广播 + app:updateInstall + LogsSettings UpdatePanel + builder publish 配置
- @ 文件提及:workspace.listFiles/readFileClip(边界/512KB/NUL 拒绝/MEMORY.md 保护),ChatInput Fuse 联想+键盘导航+附件管线注入
- MCP Resources/Prompts 发现:可选能力 try/catch 降级,mcp:listServerContents,MCPSettings 展开视图
- 文档对齐:内部 API 标准 HTML(Adapter 清单补 MiMo/已实现注记/STREAM_RESET/DONE.finishReason/ repetition_truncation 映射);README v0.8.0 亮点表

P3 测试基建:
- 新增 4 个测试文件:engine-stream-contract(6)、engine-stream-reliability(4:集成空闲超时/504 重试/思考中 abort/P4-2 强制收尾)、thinking-capability-gate(7)、pinned-proxy(9,含深校验 5)、session-trash(5,DB 域)、use-agent-stream hook 级(5)、agent.test 回放缓冲(2)
- 契约更新:orchestrator 被中断 SubAgent success=false(abort 优先级修复语义)、SSE 空 error 帧、UTF-16 正常读取、DeepSeek 未配置思考显式 disabled、迁移矩阵 v2→3
- 弱断言根治:registry WEBP 单向断言、hooks-contracts 自比恒真、memory 空 token 补强

全量验证:typecheck 0 错误 / lint 0 问题 / 系统 Node 2144 通过(301 DB 用例按 ABI 跳过)/ Electron ABI 2445/2445 全量通过 0 跳过
2026-09-05 20:06:26 +08:00

432 lines
13 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Workspace Service — 工作空间管理
*
* 负责:
* 1. 工作空间目录的创建和验证
* 2. 2 个必需磁盘文件的加载和自动创建(SOUL.md + MEMORY.md
* 3. MEMORY.md 格式校验和自动修正
*
* v0.3.14: 移除 AGENTS.md 和 USERS.md(不再读取,不再自动创建)
*
* @see docs/MetonaAI-Desktop 架构与交互设计.html — 工作空间
* @see standard/开发规范.md — 使用 fs/path 内置模块(非第三方库)
*/
import { join, resolve, sep } from 'path';
import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync, statSync } from 'fs';
import { app } from 'electron';
import log from 'electron-log';
// v0.8.0 P2-4: @ 提及文本片段的编码检测(与 read_file 同源)
import { decodeBufferWithDetection } from '../harness/tools/built-in/file-guard';
// ===== 类型定义 =====
export interface WorkspaceFiles {
soul: string;
memory: string;
}
export interface WorkspaceInfo {
path: string;
files: WorkspaceFiles;
isValid: boolean;
missingFiles: string[];
}
// ===== 常量 =====
const REQUIRED_FILES = ['SOUL.md', 'MEMORY.md'] as const;
const AUTO_CREATED_DIRS = ['logs', '.metona'] as const;
const MEMORY_TEMPLATE = `# MEMORY.md — AI 持久记忆
> 创建时间: __CREATED_AT__
> 最后更新: __UPDATED_AT__
> 工作空间: __WORKSPACE_PATH__
## 用户偏好
## 项目上下文
## 重要决策
## 待办事项
## 已知问题
`;
// ===== 服务类 =====
export class WorkspaceService {
private workspacePath: string;
private files: WorkspaceFiles = { soul: '', memory: '' };
constructor(workspacePath?: string) {
this.workspacePath =
workspacePath ?? join(app.getPath('userData'), 'MetonaWorkspaces', 'default');
}
/**
* 初始化工作空间
*
* 1. 创建目录结构
* 2. 验证/创建 2 个必需文件(SOUL.md + MEMORY.md
* 3. 加载文件内容
*/
initialize(): WorkspaceInfo {
log.info(`Initializing workspace: ${this.workspacePath}`);
// 创建工作空间目录
this.ensureDirectory(this.workspacePath);
// 创建自动目录(logs/, .metona/
for (const dir of AUTO_CREATED_DIRS) {
this.ensureDirectory(join(this.workspacePath, dir));
}
// 验证/创建必需文件(SOUL.md + MEMORY.md
const missingOnStart: string[] = [];
for (const fileName of REQUIRED_FILES) {
const filePath = join(this.workspacePath, fileName);
if (!existsSync(filePath)) {
missingOnStart.push(fileName);
this.createFile(fileName);
}
}
// 加载文件内容(在自动创建之后)
this.loadFiles();
// 校验 MEMORY.md 格式
this.validateMemoryFormat();
// isValid: 所有文件均已就绪(包含刚自动创建的)
const isValid = true;
if (missingOnStart.length > 0) {
log.info(`Auto-created missing files: ${missingOnStart.join(', ')}`);
}
log.info(`Workspace initialized: ${this.workspacePath} (valid: ${isValid})`);
return {
path: this.workspacePath,
files: { ...this.files },
isValid,
missingFiles: missingOnStart,
};
}
/**
* 获取工作空间路径
*/
getPath(): string {
return this.workspacePath;
}
/**
* 获取文件内容
*/
getFiles(): WorkspaceFiles {
return { ...this.files };
}
/**
* 重新加载文件(用户可能在外部编辑)
*/
reload(): WorkspaceFiles {
this.loadFiles();
this.validateMemoryFormat();
return { ...this.files };
}
// ===== v0.8.0 P2-4: @ 文件提及数据源 =====
/** 列出目录树时的噪声目录(依赖产物/版本库/运行时数据,永不进入提及索引) */
private static readonly MENTION_SKIP_DIRS = new Set([
'node_modules',
'.git',
'logs',
'.metona',
'dist',
'dist-electron',
'out',
'build',
'release',
]);
/**
* 工作空间文件名索引(@ 文件提及联想数据源)。
*
* 同步递归收集相对路径;跳过 MENTION_SKIP_DIRS 与隐藏目录;深度上限 8;
* 数量上限 maxFiles(防超大工作空间拖慢主进程)。FileGuard 的 MEMORY.md
* 保护不适用于"文件名索引"(只列名字不读内容)。
*/
listFiles(maxFiles = 2000): string[] {
const out: string[] = [];
const walk = (dir: string, rel: string, depth: number): void => {
if (out.length >= maxFiles || depth > 8) return;
let entries: import('fs').Dirent[];
try {
entries = readdirSync(dir, { withFileTypes: true });
} catch {
return;
}
for (const entry of entries) {
if (out.length >= maxFiles) return;
if (entry.name.startsWith('.')) continue;
if (entry.isDirectory() && WorkspaceService.MENTION_SKIP_DIRS.has(entry.name)) continue;
const relName = rel ? `${rel}/${entry.name}` : entry.name;
if (entry.isFile()) {
out.push(relName);
} else if (entry.isDirectory()) {
walk(join(dir, entry.name), relName, depth + 1);
}
}
};
walk(this.workspacePath, '', 0);
return out;
}
/**
* 读取 @ 提及文件的文本片段(≤ maxBytes,默认 512KB,二进制拒绝)。
*
* 安全:路径 resolve 后必须位于工作空间内;根目录 MEMORY.md 与工具读取
* 同口径保护(isProtectedWorkspaceFile);文本判定复用 file-guard 的
* decodeBufferWithDetectionNUL 启发式在 BOM 之后仍命中则拒绝)。
*/
readFileClip(
relPath: string,
maxBytes = 512 * 1024,
): {
success: boolean;
error?: string;
name?: string;
content?: string;
truncated?: boolean;
size?: number;
} {
try {
const resolved = resolve(this.workspacePath, relPath);
if (resolved !== this.workspacePath && !resolved.startsWith(this.workspacePath + sep)) {
return { success: false, error: 'Path escapes workspace' };
}
if (!existsSync(resolved) || !statSync(resolved).isFile()) {
return { success: false, error: 'File not found' };
}
// 根目录 MEMORY.md 保护(与 read_file 工具同口径)
if (
resolve(resolved).toLowerCase() === resolve(this.workspacePath, 'MEMORY.md').toLowerCase()
) {
return { success: false, error: 'Access to workspace root MEMORY.md is protected' };
}
const size = statSync(resolved).size;
const buf = readFileSync(resolved);
const head = buf.subarray(0, Math.min(buf.length, 8192));
// 二进制判定:非 UTF-16 BOM 前缀下含 NUL 字节 → 拒绝(文本附件语义)
const isUtf16Bom =
(head[0] === 0xff && head[1] === 0xfe) || (head[0] === 0xfe && head[1] === 0xff);
if (!isUtf16Bom) {
for (const b of head) {
if (b === 0)
return { success: false, error: 'Binary file is not supported for @ mention' };
}
}
const clipped = buf.length > maxBytes ? buf.subarray(0, maxBytes) : buf;
const { content } = decodeBufferWithDetection(clipped);
return {
success: true,
name: relPath,
content,
truncated: buf.length > maxBytes,
size,
};
} catch (err) {
return { success: false, error: (err as Error).message };
}
}
/**
* 更新 MEMORY.md 的最后更新时间戳
*/
updateMemoryTimestamp(): void {
const memoryPath = join(this.workspacePath, 'MEMORY.md');
if (!existsSync(memoryPath)) return;
let content = readFileSync(memoryPath, 'utf-8');
const now = new Date().toISOString();
content = content.replace(/> 最后更新: .*/, `> 最后更新: ${now}`);
writeFileSync(memoryPath, content, 'utf-8');
this.files.memory = content;
log.info('MEMORY.md timestamp updated');
}
/**
* 追加记忆到 MEMORY.md
*
* v0.7.2 根治: 分区头匹配此前使用 `\b` 词边界 —— JS 的 `\b` 仅对
* [A-Za-z0-9_] 有效,中文分区名(用户偏好/项目上下文/重要决策/待办事项/
* 已知问题)永远无法命中,导致 appendMemory 恒走"分区不存在"分支,
* 在文件末尾创建重复的分区头而不是插入既有分区。现改为
* `## <escaped>(?=\n|$)` 前瞻断言:分区头行必须以换行或 EOF 结束,
* 对 CJK 与拉丁分区名均正确,且防止前缀误匹配(如"用户偏好(旧)")。
*/
appendMemory(section: string, entry: string): void {
const memoryPath = join(this.workspacePath, 'MEMORY.md');
if (!existsSync(memoryPath)) return;
let content = readFileSync(memoryPath, 'utf-8');
// 查找目标 section(转义 section 名中的正则元字符)
const escaped = section.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const sectionRegex = new RegExp(`## ${escaped}(?=\\n|$)`);
const sectionMatch = content.match(sectionRegex);
if (sectionMatch) {
// 在 section 末尾追加
const sectionIndex = content.indexOf(sectionMatch[0]) + sectionMatch[0].length;
const nextSectionIndex = content.indexOf('\n## ', sectionIndex);
const insertPoint = nextSectionIndex === -1 ? content.length : nextSectionIndex;
const before = content.slice(0, insertPoint).trimEnd();
const after = content.slice(insertPoint);
content = `${before}\n- ${entry}\n${after}`;
} else {
// section 不存在,追加到文件末尾
content += `\n## ${section}\n- ${entry}\n`;
}
// 更新时间戳
content = content.replace(/> 最后更新: .*/, `> 最后更新: ${new Date().toISOString()}`);
writeFileSync(memoryPath, content, 'utf-8');
this.files.memory = content;
log.info(`MEMORY.md: appended to section "${section}"`);
}
/**
* 校验 MEMORY.md 格式
*
* 简化规则:
* 1. 元数据头 — 用 > 引用语法,包含创建时间、最后更新、工作空间
* 2. 分区结构 — 包含用户偏好、项目上下文、重要决策(其余可选)
* 3. 时间戳自动更新
*/
validateMemoryFormat(): boolean {
const memoryPath = join(this.workspacePath, 'MEMORY.md');
if (!existsSync(memoryPath)) return false;
let content = readFileSync(memoryPath, 'utf-8');
let modified = false;
// 规则 1: 元数据头(> 引用语法)
const requiredMeta: Array<{ key: string; line: string }> = [
{ key: '> 创建时间', line: `> 创建时间: ${new Date().toISOString()}` },
{ key: '> 最后更新', line: `> 最后更新: ${new Date().toISOString()}` },
{ key: '> 工作空间', line: `> 工作空间: ${this.workspacePath}` },
];
for (const meta of requiredMeta) {
if (!content.includes(meta.key)) {
// v0.3.0 修复: 检查 # MEMORY 标题是否存在,不存在则在文件开头插入
const memoryTitleIdx = content.indexOf('# MEMORY');
let titleEnd: number;
if (memoryTitleIdx === -1) {
// 标题不存在,在开头插入
titleEnd = 0;
} else {
titleEnd = content.indexOf('\n', memoryTitleIdx) + 1;
// 如果没有换行符(文件只有一行),titleEnd 为 0,在末尾插入
if (titleEnd === 0) titleEnd = content.length;
}
content = content.slice(0, titleEnd) + meta.line + '\n' + content.slice(titleEnd);
modified = true;
log.info(`MEMORY.md: auto-added missing metadata "${meta.key}"`);
}
}
// 规则 2: 核心分区
const requiredSections = ['## 用户偏好', '## 项目上下文', '## 重要决策'];
for (const section of requiredSections) {
if (!content.includes(section)) {
content += `\n${section}\n`;
modified = true;
log.info(`MEMORY.md: auto-added missing section "${section}"`);
}
}
// 规则 3: 时间戳更新
const now = new Date().toISOString();
content = content.replace(/> 最后更新: .*/, `> 最后更新: ${now}`);
if (modified) {
writeFileSync(memoryPath, content, 'utf-8');
this.files.memory = content;
log.info('MEMORY.md: format validation completed, auto-corrected');
}
return !modified;
}
// ===== 私有方法 =====
/**
* 确保目录存在
*/
private ensureDirectory(dirPath: string): void {
if (!existsSync(dirPath)) {
mkdirSync(dirPath, { recursive: true });
log.debug(`Created directory: ${dirPath}`);
}
}
/**
* 创建必需文件
*/
private createFile(fileName: string): void {
const filePath = join(this.workspacePath, fileName);
switch (fileName) {
case 'MEMORY.md': {
const now = new Date().toISOString();
const content = MEMORY_TEMPLATE.replace('__WORKSPACE_PATH__', this.workspacePath)
.replace('__CREATED_AT__', now)
.replace('__UPDATED_AT__', now);
writeFileSync(filePath, content, 'utf-8');
break;
}
case 'SOUL.md':
writeFileSync(
filePath,
'# SOUL.md — AI 灵魂定义\n\n# 用户可在此定义 Agent 的身份、性格和核心价值观\n',
'utf-8',
);
break;
}
log.info(`Auto-created file: ${fileName}`);
}
/**
* 加载所有文件内容(SOUL.md + MEMORY.md
*/
private loadFiles(): void {
this.files.soul = this.readFile('SOUL.md');
this.files.memory = this.readFile('MEMORY.md');
}
/**
* 读取单个文件
*/
private readFile(fileName: string): string {
const filePath = join(this.workspacePath, fileName);
try {
return readFileSync(filePath, 'utf-8');
} catch {
log.warn(`Failed to read file: ${filePath}`);
return '';
}
}
}