import { createHash, randomUUID } from 'node:crypto' import { closeSync, existsSync, fsyncSync, lstatSync, mkdirSync, openSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync } from 'node:fs' import { dirname, join, relative, sep } from 'node:path' import type { MemoryScope, WorkspaceMemory, WorkspaceMemoryJob, WorkspaceMemoryRevision, WorkspaceMemorySource, WorkspaceProject } from '../shared/workspaceKnowledge.js' import type { WorkspaceAgent, WorkspaceConversation } from '../shared/workspace.js' import { HttpError } from './errors.js' import type { OpenVikingService } from './openVikingService.js' import { OpenVikingMemoryProvider } from './openVikingMemoryProvider.js' import type { WorkspaceStore } from './workspaceStore.js' import { emptyMemoryContext, renderMemoryFacts, MEMORY_CONTEXT_PREFIX, MEMORY_CONTEXT_SUFFIX, type WorkspaceMemoryContext, type MemoryContextFact } from './workspaceMemoryContext.js' export const KNOWLEDGE_FEATURES = ['bot-collaboration-v1', 'bot-discussion-v1', 'bot-file-memory-v1', 'bot-projects-v1'] /** Includes scope headings, provenance and instructions, across all three scopes. */ export const MEMORY_CONTEXT_MAX_CHARS = 16_000 type Scope = { scope: MemoryScope; agentId: string; projectId?: string } type Actor = { agentId?: string } export type MemoryScopeInput = Scope export type MemoryActor = Actor export type MemoryWriteInput = Scope & { requestId: string; id?: string; expectedRevision?: number; content: string; topic?: string; tier: WorkspaceMemory['tier']; sources?: WorkspaceMemorySource[]; automatic?: boolean } export interface MemoryWriteResult { memory: WorkspaceMemory; outcome: 'written' | 'duplicate' | 'replayed' } type Write = { path: string; content: string | null } type Transaction = { id: string; writes: Write[]; event: string; data: unknown } type MemoryMetadata = Omit & { fingerprint: string } type Revision = WorkspaceMemoryRevision const terminalJobs = new Set(['complete', 'skipped']) const segment = (value: string): string => { if (!/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,127}$/.test(value)) throw new HttpError(400, '记录标识无效', 'knowledge_invalid_id') return value } const hash = (value: string): string => createHash('sha256').update(value).digest('hex') const normalize = (value: string): string => value.normalize('NFC').replace(/\s+/gu, ' ').trim() const json = (value: unknown): string => JSON.stringify(value, null, 2) + '\n' const date = (at: number): string => new Date(at).toISOString().slice(0, 10) const errorCode = (error: unknown): string | undefined => (error as NodeJS.ErrnoException)?.code /** Files are authoritative. SQLite only contains rebuildable project/event projections. */ export class WorkspaceKnowledge { readonly root: string readonly store: WorkspaceStore private readonly openViking?: OpenVikingService private locked = new Set() private readonly memoryProvider?: OpenVikingMemoryProvider onChanged: (owner: string) => void = () => {} /** Fault injection exercises recovery after a real, partially applied transaction. */ fault?: (path: string) => void constructor(home: string, store: WorkspaceStore, openViking?: OpenVikingService) { this.root = join(home, 'bot-workspace') this.store = store this.openViking = openViking if (openViking) this.memoryProvider = new OpenVikingMemoryProvider(openViking, this) for (const owner of this.names(this.root)) { if (!/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,127}$/.test(owner)) continue this.withLock(owner, () => this.recover(owner)) this.rebuild(owner) } } private get activeMemoryProvider(): OpenVikingMemoryProvider | undefined { return this.openViking?.enabled ? this.memoryProvider : undefined } private async remoteMemory(operation: string, run: () => Promise): Promise { try { return await run() } catch (error) { if (error instanceof HttpError) throw error throw new HttpError(502, `OpenViking 记忆${operation}失败:${error instanceof Error ? error.message : '未知错误'}`, 'openviking_unavailable') } } ownerDir(owner: string): string { return join(this.root, segment(owner)) } private path(owner: string, path: string): string { const root = this.ownerDir(owner), target = join(root, path), rel = relative(root, target) if (!rel || rel.startsWith('..') || rel.split(sep).some(p => p === '..')) throw new HttpError(400, '数据路径无效', 'knowledge_path_invalid') try { if (lstatSync(root).isSymbolicLink()) throw new HttpError(400, '记忆根目录不能是符号链接', 'knowledge_path_invalid') } catch (error) { if (errorCode(error) !== 'ENOENT') throw error } let current = root for (const part of rel.split(sep)) { current = join(current, part) try { if (lstatSync(current).isSymbolicLink()) throw new HttpError(400, '记忆目录不能包含符号链接', 'knowledge_path_invalid') } catch (error) { if (errorCode(error) !== 'ENOENT') throw error } } return target } private names(path: string): string[] { try { return readdirSync(path).sort() } catch (error) { if (errorCode(error) === 'ENOENT') return []; throw error } } private text(owner: string, path: string): string { try { return readFileSync(this.path(owner, path), 'utf8') } catch (error) { if (errorCode(error) === 'ENOENT') return ''; throw error } } private read(owner: string, path: string, fallback: T): T { const text = this.text(owner, path) if (!text) { if (existsSync(this.path(owner, path))) throw new HttpError(409, '记忆或项目文件为空,请从备份恢复', 'knowledge_file_corrupt') return fallback } try { return JSON.parse(text) as T } catch { throw new HttpError(409, '记忆或项目文件损坏,请从备份恢复', 'knowledge_file_corrupt') } } private atomicFile(owner: string, path: string, content: string | null): void { const target = this.path(owner, path) if (content === null) { rmSync(target, { force: true }); return } mkdirSync(dirname(target), { recursive: true, mode: 0o700 }) const temporary = `${target}.${randomUUID()}.tmp`, fd = openSync(temporary, 'wx', 0o600) try { writeFileSync(fd, content, 'utf8'); fsyncSync(fd) } finally { closeSync(fd) } renameSync(temporary, target) const directory = openSync(dirname(target), 'r') try { fsyncSync(directory) } finally { closeSync(directory) } } private withLock(owner: string, fn: () => T): T { if (this.locked.has(owner)) throw new HttpError(409, '记忆正在更新,请稍后重试', 'knowledge_busy') const root = this.ownerDir(owner) mkdirSync(root, { recursive: true, mode: 0o700 }) if (lstatSync(root).isSymbolicLink()) throw new HttpError(400, '记忆根目录不能是符号链接', 'knowledge_path_invalid') const lock = join(root, '.write-lock') try { mkdirSync(lock, { mode: 0o700 }) } catch (error) { if (errorCode(error) !== 'EEXIST') throw error let pid = 0 try { pid = Number(readFileSync(join(lock, 'pid'), 'utf8')); if (pid > 0) process.kill(pid, 0) } catch (failure) { if (errorCode(failure) === 'ESRCH') { rmSync(lock, { recursive: true }); return this.withLock(owner, fn) } if (errorCode(failure) === 'ENOENT' && Date.now() - lstatSync(lock).mtimeMs > 30000) { rmSync(lock, { recursive: true }); return this.withLock(owner, fn) } } throw new HttpError(409, '记忆正由另一进程更新,请稍后重试', 'knowledge_busy') } writeFileSync(join(lock, 'pid'), String(process.pid), { mode: 0o600 }) this.locked.add(owner) try { return fn() } finally { this.locked.delete(owner); rmSync(lock, { recursive: true, force: true }) } } private apply(owner: string, tx: Transaction): void { for (const write of tx.writes) { this.atomicFile(owner, write.path, write.content); this.fault?.(write.path) } this.store.atomic(() => { this.rebuild(owner) if (!this.store.get(owner, 'knowledge-transaction', tx.id)) { this.store.event(owner, tx.event, tx.data) this.store.put(owner, 'knowledge-transaction', tx.id, { id: tx.id }) } }) this.atomicFile(owner, `.transactions/${tx.id}.json`, null) } private recover(owner: string): void { for (const name of this.names(join(this.ownerDir(owner), '.transactions')).filter(n => n.endsWith('.json'))) { const tx = this.read(owner, `.transactions/${name}`, null) if (!tx || !Array.isArray(tx.writes) || typeof tx.id !== 'string') throw new HttpError(409, '记忆恢复日志损坏', 'knowledge_file_corrupt') this.apply(owner, tx) } } private command(owner: string, requestId: string, input: unknown, event: string, operation: () => { result: T; writes: Write[] }): T { segment(requestId) return this.withLock(owner, () => { this.recover(owner) const path = `.commands/${requestId}.json`, fingerprint = hash(JSON.stringify(input)), saved = this.read<{ fingerprint: string; result: T } | null>(owner, path, null) if (saved) { if (saved.fingerprint !== fingerprint) throw new HttpError(409, '请求标识已用于其他修改', 'request_conflict') return saved.result } const { result, writes } = operation() const tx: Transaction = { id: randomUUID(), writes: [...writes, { path, content: json({ fingerprint, result }) }], event, data: result } this.atomicFile(owner, `.transactions/${tx.id}.json`, json(tx)) this.apply(owner, tx) this.onChanged(owner) return result }) } agent(owner: string, id: string): WorkspaceAgent { const agent = this.store.require(owner, 'agent', segment(id)) if (agent.archived) throw new HttpError(409, '机器人已归档', 'agent_archived') return agent } projects(owner: string): WorkspaceProject[] { return this.names(join(this.ownerDir(owner), 'projects')).filter(id => /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,127}$/.test(id)).flatMap(id => { const text = this.text(owner, `projects/${id}/project.md`) if (!text) return [] const match = /^---\n([\s\S]*?)\n---\n([\s\S]*)$/.exec(text) if (!match) throw new HttpError(409, '项目文件格式损坏', 'knowledge_file_corrupt') const meta: Record = {} try { for (const line of match[1]!.split('\n')) { const split = line.indexOf(':'); if (split < 1) throw new Error(); meta[line.slice(0, split)] = JSON.parse(line.slice(split + 1).trim()) } } catch { throw new HttpError(409, '项目文件格式损坏', 'knowledge_file_corrupt') } const memberIds = this.names(join(this.ownerDir(owner), 'agents')).filter(agentId => /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,127}$/.test(agentId) && this.memberships(owner, agentId).includes(id)) return [{ ...meta, id, description: match[2]!.trim(), memberIds, groupIds: this.read<{ groups: string[] }>(owner, `projects/${id}/groups.json`, { groups: [] }).groups } as unknown as WorkspaceProject] }) } project(owner: string, id: string): WorkspaceProject { const project = this.projects(owner).find(p => p.id === segment(id)) if (!project) throw new HttpError(404, '项目不存在', 'project_not_found') return project } memberships(owner: string, agentId: string): string[] { return this.read<{ projects: string[] }>(owner, `agents/${segment(agentId)}/projects.json`, { projects: [] }).projects } requireProjectMember(owner: string, projectId: string, agentId: string): WorkspaceProject { const project = this.project(owner, projectId) if (project.archived || !project.memberIds.includes(agentId)) throw new HttpError(403, '当前机器人没有此项目的权限', 'project_forbidden') return project } saveProject(owner: string, input: { requestId: string; id?: string; expectedRevision?: number; name: string; description: string; memberIds: string[]; groupIds: string[]; archived?: boolean }, actor: Actor = {}): WorkspaceProject { if (actor.agentId && this.agent(owner, actor.agentId).temporaryGoalId) throw new HttpError(403, '临时助手不能管理项目', 'project_forbidden') const id = input.id ? segment(input.id) : input.requestId return this.command(owner, input.requestId, { operation: 'project.save', input, actor }, 'project.changed', () => { const old = input.id ? this.project(owner, id) : undefined if (old && old.revision !== input.expectedRevision) throw new HttpError(409, '项目已更新,请重新读取后保存', 'project_revision_conflict') if (actor.agentId && old && !old.memberIds.includes(actor.agentId)) throw new HttpError(403, '只能管理自己参与的项目', 'project_forbidden') const memberIds = [...new Set(input.memberIds)], groupIds = [...new Set(input.groupIds)] for (const agentId of memberIds) if (this.agent(owner, agentId).temporaryGoalId) throw new HttpError(403, '临时助手不能加入持久项目', 'helper_task_bound') for (const groupId of groupIds) { const group = this.store.require(owner, 'conversation', groupId) if (group.kind !== 'group' || group.archived || group.memberIds.some(member => !memberIds.includes(member))) throw new HttpError(400, '关联群的全部成员需要先加入项目', 'project_group_members') if (this.projects(owner).some(p => p.id !== id && p.groupIds.includes(groupId))) throw new HttpError(409, '此群已关联其他项目', 'project_group_linked') } const changingGroups = [...new Set([...(old?.groupIds ?? []), ...groupIds])] if (this.store.list<{ conversationId: string; status: string }>(owner, 'run').some(run => changingGroups.includes(run.conversationId) && !['complete', 'failed', 'interrupted'].includes(run.status))) throw new HttpError(409, '请等关联群当前执行结束后修改项目', 'project_busy') if (this.store.list<{ conversationId: string; status: string }>(owner, 'goal').some(goal => changingGroups.includes(goal.conversationId) && !['complete', 'blocked', 'cancelled'].includes(goal.status))) throw new HttpError(409, '请先结束关联群的交付目标', 'project_busy') const now = Date.now(), result: WorkspaceProject = { id, name: input.name.trim(), description: input.description.trim(), memberIds, groupIds, revision: (old?.revision ?? 0) + 1, archived: input.archived ?? old?.archived ?? false, createdAt: old?.createdAt ?? now, updatedAt: now } const { description, memberIds: _members, groupIds: _groups, ...metadata } = result const writes: Write[] = [{ path: `projects/${id}/project.md`, content: `---\n${Object.entries(metadata).map(([key, value]) => `${key}: ${JSON.stringify(value)}`).join('\n')}\n---\n${description}\n` }, { path: `projects/${id}/groups.json`, content: json({ groups: groupIds }) }] for (const agentId of new Set([...(old?.memberIds ?? []), ...memberIds])) writes.push({ path: `agents/${segment(agentId)}/projects.json`, content: json({ projects: [...new Set([...this.memberships(owner, agentId).filter(p => p !== id), ...(memberIds.includes(agentId) ? [id] : [])])].sort() }) }) return { result, writes } }) } private rebuild(owner: string): void { const projects = this.projects(owner) for (const old of this.store.list<{ id: string }>(owner, 'project')) if (!projects.some(p => p.id === old.id)) this.store.remove(owner, 'project', old.id) for (const project of projects) this.store.put(owner, 'project', project.id, project) for (const conversation of this.store.list(owner, 'conversation').filter(c => c.kind === 'group')) { const projectId = projects.find(p => !p.archived && p.groupIds.includes(conversation.id))?.id if (conversation.projectId !== projectId) { conversation.projectId = projectId this.store.put(owner, 'conversation', conversation.id, conversation) this.store.event(owner, 'conversation.changed', conversation, conversation.id) } } } private shard(scope: Scope): string { const agent = segment(scope.agentId) if (scope.scope === 'agent') return `agents/${agent}/memory` if (scope.scope === 'user') return `user-memory/agents/${agent}` if (!scope.projectId) throw new HttpError(400, '需要选择项目', 'project_required') return `projects/${segment(scope.projectId)}/memory/agents/${agent}` } authorizeMemory(owner: string, scope: Scope, actor: Actor, write = false): void { this.agent(owner, scope.agentId) if (actor.agentId) { const agent = this.agent(owner, actor.agentId) if (agent.temporaryGoalId) throw new HttpError(403, '临时助手不直接维护长期记忆', 'helper_task_bound') if ((write || scope.scope === 'agent') && actor.agentId !== scope.agentId) throw new HttpError(403, '只能维护自己贡献的记忆', 'memory_forbidden') if (scope.scope === 'project') this.requireProjectMember(owner, scope.projectId!, actor.agentId) } if (scope.scope === 'project') this.project(owner, scope.projectId!) } private shardMemories(owner: string, scope: Scope): WorkspaceMemory[] { const shard = this.shard(scope), metadata = this.read(owner, `${shard}/.dreaming/records.json`, []) const paths = ['profile.md', ...this.names(join(this.ownerDir(owner), shard, 'log')).filter(n => /^\d{4}-\d{2}\.md$/.test(n)).map(n => `log/${n}`)] return paths.flatMap(path => { const text = this.text(owner, `${shard}/${path}`) return text.split('\n').filter(line => line.trim() && !line.startsWith('#') && !line.startsWith('