workspaceMemorySynthesis.test.ts 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // @vitest-environment node
  2. import { afterEach, beforeEach, expect, it, vi } from 'vitest'
  3. import { mkdtempSync, rmSync } from 'node:fs'
  4. import { join } from 'node:path'
  5. import { tmpdir } from 'node:os'
  6. import { randomUUID } from 'node:crypto'
  7. import { WorkspaceStore } from '../../src/server/workspaceStore'
  8. import { WorkspaceRuntime } from '../../src/server/workspaceRuntime'
  9. import { MEMORY_EXTRACTION_MAX_CHARS, WorkspaceMemorySynthesis } from '../../src/server/workspaceMemorySynthesis'
  10. import { UploadStore } from '../../src/server/uploads'
  11. import type { WorkspaceNodes } from '../../src/server/workspaceGateway'
  12. import type { WorkspaceAgent, WorkspaceConversation, WorkspaceMessage, WorkspaceRun } from '../../src/shared/workspace'
  13. const owner = 'synthesis-owner'
  14. let home: string, store: WorkspaceStore, runtime: WorkspaceRuntime, synthesis: WorkspaceMemorySynthesis, uploads: UploadStore, agent: WorkspaceAgent
  15. beforeEach(() => {
  16. home = mkdtempSync(join(tmpdir(), 'yaoyao-synthesis-')); store = new WorkspaceStore(home); uploads = new UploadStore(home)
  17. runtime = new WorkspaceRuntime(store, { requireSource: () => {} } as unknown as WorkspaceNodes, uploads)
  18. vi.spyOn(runtime, 'wake').mockImplementation(() => {})
  19. synthesis = new WorkspaceMemorySynthesis(runtime)
  20. agent = store.createAgent(owner, { name: '记忆成员', profile: 'default' })
  21. })
  22. afterEach(() => { synthesis.close(); runtime.close(); uploads.close(); store.close(); rmSync(home, { recursive: true, force: true }) })
  23. function turn(content = '以后我喜欢简洁的中文回复') {
  24. const c = store.list<WorkspaceConversation>(owner, 'conversation')[0]!
  25. const run = runtime.send(owner, c.id, { requestId: randomUUID(), content })
  26. const reply: WorkspaceMessage = { id: randomUUID(), conversationId: c.id, seq: 0, role: 'assistant', agentId: agent.id, content: '将按此偏好回答', reasoning: 'private-execution-text', status: 'complete', runId: run.id, tools: [{ name: 'private-tool' }], attachments: [], createdAt: Date.now() }
  27. store.saveMessage(owner, reply); run.status = 'complete'; store.saveRun(owner, run)
  28. synthesis.enqueue(owner, run)
  29. return { run, source: store.require<WorkspaceMessage>(owner, 'message', run.messageId), reply }
  30. }
  31. it('runs extraction on server Hermes even for a legacy computer Bot',async()=>{
  32. const request=vi.fn(async()=>({status:200,body:Buffer.from(JSON.stringify({text:'{"memories":[]}'}))}))
  33. const target={session:{request}}
  34. runtime.nodes.target=vi.fn(()=>target) as any
  35. runtime.nodes.targetForAgent=vi.fn(()=>{throw new Error('VM must not run inference')})
  36. vi.spyOn(runtime,'botCapabilities').mockResolvedValue({memory:true,extraction:true} as any)
  37. await synthesis.infer(owner,{...agent,execution:'computer'},'extract facts')
  38. expect(runtime.nodes.target).toHaveBeenCalledWith(owner,agent.nodeId)
  39. expect(runtime.nodes.targetForAgent).not.toHaveBeenCalled()
  40. })
  41. it.each([{ kind: 'text', content: '正文'.repeat(6000) }, { kind: 'escaped Unicode', content: '\u0000"\\😀'.repeat(2000) }])('keeps long $kind evidence valid JSON within the complete request budget', async ({ content }) => {
  42. const { source, reply, run } = turn(content)
  43. const project = runtime.knowledge.saveProject(owner, { requestId: randomUUID(), name: '项目', description: '', memberIds: [agent.id], groupIds: [] })
  44. run.projectId = project.id; store.saveRun(owner, run)
  45. const job = runtime.knowledge.jobs(owner)[0]!
  46. runtime.knowledge.saveJob(owner, { ...job, projectId: project.id })
  47. for (const scope of ['agent', 'user', 'project'] as const) {
  48. for (let index = 0; index < 6; index++) await runtime.knowledge.writeMemory(owner, {
  49. requestId: randomUUID(), scope, agentId: agent.id, projectId: scope === 'project' ? project.id : undefined,
  50. content: `${scope}-${index}:`.padEnd(2000, '甲'), tier: 'profile',
  51. })
  52. }
  53. for (let index = 0; index < 6; index++) store.saveMessage(owner, { ...reply, id: randomUUID(), seq: 0, content })
  54. const infer = vi.fn(async (_owner, _agent, prompt: string) => {
  55. expect(prompt.length).toBeLessThanOrEqual(MEMORY_EXTRACTION_MAX_CHARS)
  56. const serialized = prompt.slice(prompt.indexOf('本轮证据:') + '本轮证据:'.length)
  57. const evidence = JSON.parse(serialized)
  58. expect(evidence[0].id).toBe(source.id)
  59. expect(evidence[0].content.length).toBeGreaterThan(0)
  60. expect(evidence.every((message: { content: string }) => source.content.includes(message.content) || message.content === reply.content)).toBe(true)
  61. expect(prompt).not.toContain('private-execution-text')
  62. return '{"memories":[]}'
  63. })
  64. synthesis.infer = infer
  65. await synthesis.tick()
  66. expect(infer).toHaveBeenCalledOnce()
  67. expect(runtime.knowledge.jobs(owner)[0]?.status).toBe('complete')
  68. })
  69. it('merges one agent’s visible results per user request and retains pending jobs across restart', async () => {
  70. const { run, source, reply } = turn()
  71. store.saveMessage(owner, { ...reply, id: randomUUID(), seq: 0, content: '另一条公开结果' })
  72. synthesis.enqueue(owner, run)
  73. expect(runtime.knowledge.jobs(owner)).toHaveLength(1)
  74. synthesis.close(); synthesis = new WorkspaceMemorySynthesis(runtime)
  75. const infer = vi.fn(async (_owner, _agent, prompt: string) => {
  76. expect(prompt).not.toContain('private-execution-text'); expect(prompt).not.toContain('private-tool')
  77. return JSON.stringify({ memories: [{ scope: 'user', content: '用户偏好简洁中文', tier: 'profile', sourceMessageId: source.id, quote: source.content }] })
  78. })
  79. synthesis.infer = infer
  80. await synthesis.tick()
  81. expect(infer).toHaveBeenCalledTimes(1)
  82. expect((await runtime.knowledge.memories(owner, { scope: 'user' }))[0]?.content).toBe('用户偏好简洁中文')
  83. expect(runtime.knowledge.jobs(owner)[0]?.status).toBe('complete')
  84. await synthesis.tick(); expect(infer).toHaveBeenCalledTimes(1)
  85. })
  86. it('rejects fabricated evidence and does not promote an assistant assertion into shared user memory', async () => {
  87. const { source, reply } = turn('请完成这一次的接口分析')
  88. synthesis.infer = async () => JSON.stringify({ memories: [
  89. { scope: 'agent', content: '不存在的事实', tier: 'log', sourceMessageId: source.id, quote: '证据里没有这句话' },
  90. { scope: 'user', content: '用户是架构师', tier: 'profile', sourceMessageId: reply.id, quote: reply.content },
  91. { scope: 'project', content: '无项目不应保存', tier: 'log', sourceMessageId: source.id, quote: source.content },
  92. ] })
  93. await synthesis.tick()
  94. expect(await runtime.knowledge.memories(owner, { scope: 'user' })).toEqual([])
  95. expect(await runtime.knowledge.memories(owner, { scope: 'agent', agentId: agent.id })).toEqual([])
  96. })
  97. it('honors pause during inference and retries failures without breaking the conversation', async () => {
  98. const { run } = turn()
  99. synthesis.infer = async () => { throw new Error('temporary inference outage') }
  100. await synthesis.tick()
  101. const failed = runtime.knowledge.jobs(owner)[0]!
  102. expect(failed.status).toBe('pending'); expect(failed.attempts).toBe(1)
  103. expect(store.require<WorkspaceRun>(owner, 'run', run.id).status).toBe('complete')
  104. runtime.knowledge.saveJob(owner, { ...failed, nextAt: 0 })
  105. synthesis.infer = async () => { store.updateAgent(owner, agent.id, { memoryEnabled: false }); return '{"memories":[]}' }
  106. await synthesis.tick()
  107. expect(runtime.knowledge.jobs(owner)[0]?.status).toBe('skipped')
  108. })
  109. it('does not recreate a forgotten fact by paraphrasing evidence from the same old turn', async () => {
  110. const { source } = turn()
  111. const fact = await runtime.knowledge.writeMemory(owner, { requestId: randomUUID(), scope: 'agent', agentId: agent.id, content: '偏好简洁中文', tier: 'profile', sources: [{ messageId: source.id, conversationId: source.conversationId, quote: source.content }] })
  112. await runtime.knowledge.forget(owner, { requestId: randomUUID(), scope: 'agent', agentId: agent.id, id: fact.id, expectedRevision: fact.revision })
  113. synthesis.infer = async () => JSON.stringify({ memories: [{ scope: 'agent', content: '用中文简单回答用户', tier: 'profile', sourceMessageId: source.id, quote: source.content }] })
  114. await synthesis.tick()
  115. expect(await runtime.knowledge.memories(owner, { scope: 'agent', agentId: agent.id })).toEqual([])
  116. })
  117. it('waits for asynchronous collaboration and extracts the returned result only in its originating context', async () => {
  118. const conversation = store.list<WorkspaceConversation>(owner, 'conversation')[0]!
  119. const root = runtime.send(owner, conversation.id, { requestId: randomUUID(), content: '请联系同伴核对项目规范并记住结果' })
  120. root.status = 'complete'; store.saveRun(owner, root)
  121. const peerId = randomUUID(), replyRunId = randomUUID()
  122. store.put(owner, 'collaboration-chain', root.id, { id: root.id, rootRunId: root.id, stopped: false })
  123. const peer = { id: peerId, chainId: root.id, status: 'running', conversationId: conversation.id, runId: replyRunId }
  124. store.put(owner, 'peer-message', peerId, peer)
  125. synthesis.enqueue(owner, root)
  126. expect(runtime.knowledge.jobs(owner)).toEqual([])
  127. const reply: WorkspaceRun = { ...root, id: replyRunId, triggerKind: 'peer', peerMessageId: peerId, collaborationChainId: root.id }
  128. store.saveRun(owner, reply)
  129. store.put(owner, 'peer-message', peerId, { ...peer, status: 'complete' })
  130. const message: WorkspaceMessage = { id: randomUUID(), conversationId: conversation.id, seq: 0, role: 'assistant', agentId: agent.id, content: '核对结果:接口需要保持向后兼容', reasoning: '', status: 'complete', runId: replyRunId, tools: [], attachments: [], createdAt: Date.now() }
  131. store.saveMessage(owner, message)
  132. synthesis.enqueue(owner, reply)
  133. expect(runtime.knowledge.jobs(owner)).toHaveLength(1)
  134. synthesis.infer = async (_owner, _agent, prompt) => {
  135. expect(prompt).toContain(message.content)
  136. return JSON.stringify({ memories: [{ scope: 'agent', content: '接口需要保持向后兼容', tier: 'log', sourceMessageId: message.id, quote: message.content }] })
  137. }
  138. await synthesis.tick()
  139. expect((await runtime.knowledge.memories(owner, { scope: 'agent', agentId: agent.id }))[0]?.content).toBe('接口需要保持向后兼容')
  140. })