import assert from 'node:assert/strict' import { createMnemonMemoryRemote } from './lib/memory/mnemon-remote.js' import { createMemoryTools } from './lib/memory/tools.js' import { mnemonReadFromContext, mnemonToolsFromContext } from './lib/mnemon/read-service.js' const calls = [] const writes = new Set() const runtime = [{ target: 'user', content: 'Runtime fact', created_at: '2026-01-01T00:00:00Z' }] const fact = { id: '1', memoryBodyId: 'body', content: 'Project fact', category: 'fact' } const ok = value => ({ ok: true, value }) const bridge = { workspaces: () => [{ id: 'ws-1', name: 'Project One', path: 'C:/test' }], async read(endpoint, payload) { calls.push([endpoint, payload]) if (endpoint === 'runtime-memory') return ok({ entries: runtime }) if (endpoint === 'search') return ok({ results: [fact], mode: 'keyword' }) if (endpoint === 'list') return ok({ items: [fact], total: 1 }) if (endpoint === 'memory-exact') return ok(fact) if (endpoint === 'body-directory') return ok({ items: [{ id: 'body', active: true, mnemonDefault: true, provider: { capabilities: { remember: true } } }] }) throw new Error('unexpected read') }, async append(endpoint, payload) { calls.push([endpoint, payload]) if (endpoint === 'runtime-memory') { if (!writes.has(payload.operationId)) runtime.unshift({ target: 'memory', content: payload.content, created_at: '2026-02-01T00:00:00Z' }) writes.add(payload.operationId) return ok({ success: true, added: payload.content }) } return ok({ ...fact, committed: payload.content !== 'Rejected' }) }, } const standalone = { read: async () => ok('standalone') } const bundled = { read: async () => ok('bundled'), appendBrainMemory: bridge.append } const hostServices = new Map([['mnemonRemote', standalone], ['workspaceRegistry', { list: bridge.workspaces }]]) const ctx = { get: (name, strict) => { assert.equal(strict, false); return hostServices.get(name) } } assert.equal(mnemonToolsFromContext(ctx), undefined) assert.deepEqual(await mnemonReadFromContext(ctx).read('status', {}), ok('standalone')) hostServices.set('brainMnemonHost', bundled) assert.deepEqual(await mnemonReadFromContext(ctx).read('status', {}), ok('bundled')) assert.deepEqual(mnemonToolsFromContext(ctx).workspaces(), bridge.workspaces()) assert.equal(mnemonReadFromContext(ctx).workspaceIdForPath('C:/test/./'), 'ws-1') assert.equal(mnemonReadFromContext(ctx).workspaceIdForPath('C:/test/other'), undefined) let active = bridge let enabled = true const remote = createMnemonMemoryRemote(() => active) const tools = new Map(createMemoryTools({ remote: () => remote, settings: () => ({ enabled, defaultK: 20 }) }).map(tool => [tool.name, tool])) const call = (name, args) => tools.get('memory_' + name).execute(args) const runtimeMatch = (await call('search', { query: 'FACT' })).matches[0] assert.deepEqual(calls.at(-1), ['runtime-memory', {}]) assert.equal(runtimeMatch.project_id, null) assert.equal((await call('read', { id: runtimeMatch.id })).content, 'Runtime fact') assert.deepEqual(await call('projects', {}), { projects: [{ id: 'ws-1', name: 'Project One' }] }) const projectMatch = (await call('search', { query: 'fact', project_id: 'ws-1' })).matches[0] assert.deepEqual(calls.at(-1), ['search', { workspaceId: 'ws-1', query: 'fact', mode: 'keyword', limit: 20 }]) assert.equal(projectMatch.project_id, 'ws-1') assert.equal(projectMatch.category, 'fact') assert.equal((await call('read', { id: projectMatch.id })).content, 'Project fact') assert.deepEqual(calls.at(-1), ['memory-exact', { workspaceId: 'ws-1', memoryBodyId: 'body', id: '1' }]) assert.equal((await call('list', { project_id: 'ws-1' })).items[0].id, projectMatch.id) assert.deepEqual(calls.at(-1), ['list', { workspaceId: 'ws-1', limit: 1000 }]) const beforeInvalid = calls.length await assert.rejects(call('list', { project_id: 'C:/test' }), /工作区/) await assert.rejects(call('list', { project_id: '' }), /project_id/) await assert.rejects(call('add', { content: 'Fact', operation_id: 'bad' }), /UUID/) await assert.rejects(call('add', { content: 'Fact', tags: ['tag'] }), /不支持标签/) assert.equal(calls.length, beforeInvalid) const operation_id = '9be2f2d1-f2e8-5ae9-bac2-7eab78ba41fa' const saved = await call('add', { content: 'Added Runtime fact', operation_id }) assert.deepEqual(await call('add', { content: 'Added Runtime fact', operation_id }), saved) assert.deepEqual(saved, { id: saved.id, operation_id, state: 'saved' }) assert.equal((await call('list', { offset: 1, limit: 1 })).items[0].id, runtimeMatch.id) assert.equal((await call('read', { id: saved.id })).content, 'Added Runtime fact') const projectOperation = '9be2f2d1-f2e8-5ae9-bac2-7eab78ba41fb' const projectSaved = await call('add', { content: 'Project fact', project_id: 'ws-1', tags: ['tag'], sector: 'semantic', operation_id: projectOperation }) assert.equal(projectSaved.id, projectMatch.id) assert.deepEqual(calls.at(-1), ['remember', { workspaceId: 'ws-1', memoryBodyId: 'body', content: 'Project fact', tags: ['tag'], category: 'fact', operationId: projectOperation }]) await assert.rejects(call('add', { content: 'Rejected', project_id: 'ws-1' }), /可验证/) active = undefined await assert.rejects(call('search', { query: 'fact' }), /没有可用的 Mnemon/) active = bridge assert.equal((await call('list', {})).total, 2) enabled = false await assert.rejects(call('list', {}), /已禁用/) console.log('PASS Brain memory tools use the live Mnemon view sources and verify append results')