import assert from 'node:assert/strict' import { register } from 'node:module' import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import test from 'node:test' // Q04 真实 memory.js 集成:注册 peer stub loader 后加载 ds-bot 的 install。 register(new URL('./stubs/peer-loader.mjs', import.meta.url).href) const { install } = await import('../../ds-bot/src/host/memory.js') function makeRuntime({ home, allowGroup }) { const handlers = {} const ctx = { get(name, fallback) { if (name === 'sansheng-governance') { return { checkMemoryScope({ scope, roomId, targetBotId }) { if (scope === 'group' && roomId === 'rb') return allowGroup() ? { allow: true } : { allow: false, reason: 'blocked-group' } if (scope === 'bot' && targetBotId === 'b') return { allow: false, reason: 'blocked-target' } return { allow: true } }, } } return fallback }, on(name, fn) { handlers[name] = fn; return () => {} }, effect(fn) { try { return fn() } catch { return () => {} } }, systemPrompt: { section: () => () => {} }, llm: { resolveModelInfo: async () => undefined }, } const bots = { a: { id: 'a', name: 'A', model: 'p/m' }, b: { id: 'b', name: 'B', model: 'p/m' } } const rooms = { rb: { id: 'rb', name: 'B group', members: ['a', 'b'], admin: 'b', sessions: {} } } const rt = { ctx, config: { memoryReview: false }, home, state: { prefs: {}, bots: {}, rooms: {}, privateArchives: [] }, teamTools: [], groupOwners: new Map([['sess-a', { roomId: 'rb' }]]), pastOwners: new Map(), botOf: id => bots[id], roomOf: id => rooms[id], selfOf: id => (id === 'sess-a' ? 'a' : undefined), isMain: () => false, isPrivate: () => false, readOnly: () => undefined, warn: () => {}, redactSecrets: text => text, load: async () => {}, } install(rt) return { rt, handlers, recall: rt.teamTools.find(tool => tool.name === 'recall') } } test('Q04:真实 memory install 不注入被拒群记忆,允许时才读取', async () => { const home = await mkdtemp(join(tmpdir(), 'sansheng-q04-')) try { await mkdir(join(home, 'memory', 'groups', 'rb'), { recursive: true }) await writeFile(join(home, 'memory', 'groups', 'rb', 'MEMORY.md'), '- B_SECRET_PROJECT_B [added: 2026-01-01]\n', 'utf8') let allowGroup = false const { handlers, recall } = makeRuntime({ home, allowGroup: () => allowGroup }) const hook = handlers['agent/pre-step'] const agent = { id: 'sess-a', session: { deriveMessages: () => [] } } const next = async () => ({ kind: 'continue', messages: [{ role: 'user', source: { kind: 'user' }, content: [{ type: 'text', text: 'hello' }] }] }) const denied = await hook({ agent }, next) const deniedText = denied.messages.map(message => (message.content ?? []).map(part => part.text).join('')).join('\n') assert.doesNotMatch(deniedText, /B_SECRET_PROJECT_B/) assert.equal((await recall.execute({ scope: 'group', query: 'B_SECRET' }, { agent: { id: 'sess-a' } })), 'blocked-group') allowGroup = true const allowed = await hook({ agent }, next) const allowedText = allowed.messages.map(message => (message.content ?? []).map(part => part.text).join('')).join('\n') assert.match(allowedText, /B_SECRET_PROJECT_B/) assert.match(await recall.execute({ scope: 'group', query: 'B_SECRET' }, { agent: { id: 'sess-a' } }), /B_SECRET_PROJECT_B/) } finally { await rm(home, { recursive: true, force: true }) } })