/** * Pattern write-back on the memory contract: which kind, importance, source * and scope each extracted memory gets, and the self-incapacity sign gate. */ import { describe, it, expect, beforeEach } from 'vitest'; import { createInMemoryMemoryStore, PERSONAL_SCOPE, workspaceScope, type MemoryRecord, type MemoryStore, } from '@waggle/memory-contract'; import { runPatternWriteBack } from '../src/pattern-write-back.js'; describe('runPatternWriteBack', () => { let memory: MemoryStore; const WS = workspaceScope('ws-1'); const inScope = async (scope = WS): Promise => memory.list({ scopes: [scope] }); const find = (records: MemoryRecord[], prefix: string): MemoryRecord | undefined => records.find(r => r.text.startsWith(prefix)); beforeEach(() => { memory = createInMemoryMemoryStore(); }); it('routes preferences and corrections to personal memory, decisions to the workspace', async () => { const deps = { memory, workspaceId: 'ws-1' }; await runPatternWriteBack(deps, 'I prefer short status updates with a clear next step', 'Understood.'); await runPatternWriteBack(deps, 'No, actually the launch owner is Marko, not Ana.', 'Thanks for the correction.'); await runPatternWriteBack(deps, 'Sounds good, go ahead with that plan.', "Let's go with the staged rollout for the beta."); const personal = await inScope(PERSONAL_SCOPE); expect(find(personal, 'User preference:')).toMatchObject({ kind: 'preference', importance: 'normal', source: 'user_stated', origin: 'chat-write-back', }); expect(find(personal, 'Correction from user:')).toMatchObject({ kind: 'fact', importance: 'important', source: 'user_stated', }); const workspace = await inScope(); expect(find(workspace, 'Decision:')).toMatchObject({ text: "Decision: Let's go with the staged rollout for the beta", kind: 'decision', importance: 'important', source: 'agent_inferred', }); expect(find(personal, 'Decision:')).toBeUndefined(); }); it('writes decisions to personal memory when no workspace is active', async () => { await runPatternWriteBack( { memory, workspaceId: null }, "Let's go with Postgres for the analytics store.", 'Good choice.', ); expect(find(await inScope(PERSONAL_SCOPE), 'Decision:')?.kind).toBe('decision'); }); it('saves research findings as an important workspace summary', async () => { const assistant = [ '## Market size', 'The segment grew 12% last year, see https://example.com/report for the numbers.', '## Competitors', 'Three vendors lead the segment: https://example.com/vendors lists them.', '## Pricing', 'Per-seat pricing dominates; usage pricing is rising (https://example.com/pricing).', 'x'.repeat(400), ].join('\n'); await runPatternWriteBack({ memory, workspaceId: 'ws-1' }, 'Research the market for workspace AI agents please', assistant); expect(find(await inScope(), 'Research findings:')).toMatchObject({ kind: 'summary', importance: 'important', source: 'agent_inferred', }); }); it("keeps the agent's own extracts as temporary workspace notes", async () => { const assistant = [ '# Rollout plan', 'I recommend shipping the beta to the design team first.', '- Freeze the feature set on Monday and cut the release branch', '- Run the migration rehearsal on the staging copy of the data', '- Announce the beta in the Friday all-hands with the known issues', 'y'.repeat(400), ].join('\n'); await runPatternWriteBack({ memory, workspaceId: 'ws-1' }, 'How should we roll out the beta to the company?', assistant); const notes = await inScope(); expect(notes.length).toBeGreaterThanOrEqual(2); for (const prefix of ['Recommendation:', 'Rollout plan:']) { expect(find(notes, prefix)).toMatchObject({ kind: 'note', importance: 'temporary', source: 'agent_inferred' }); } }); it('downgrades a self-incapacity assertion to temporary (sign gate)', async () => { await runPatternWriteBack( { memory, workspaceId: 'ws-1' }, "We decided to ship it, but you'll need to run npm install on your machine first.", 'Noted.', ); expect(find(await inScope(), 'Decision:')).toMatchObject({ kind: 'decision', importance: 'temporary' }); }); it('records the chat session and the execution trace', async () => { await runPatternWriteBack( { memory, workspaceId: 'ws-1' }, "Let's go with weekly releases from now on.", 'Good.', { sessionId: 'chat-42', traceId: 'trace-7' }, ); const decision = find(await inScope(), 'Decision:'); expect(decision).toMatchObject({ sessionId: 'chat-42', metadata: { trace_id: 'trace-7' } }); }); it('reports a style note once, however often the signal repeats', async () => { const deps = { memory, workspaceId: null }; const first = await runPatternWriteBack(deps, 'Could you put the comparison into a table for me?', 'Sure.'); const second = await runPatternWriteBack(deps, 'Again, a table with columns would help here as well.', 'Sure.'); expect(first).toEqual(['Style note: User prefers tabular data presentation']); expect(second).toEqual([]); expect((await inScope(PERSONAL_SCOPE)).filter(r => r.text.startsWith('Style note'))).toHaveLength(1); }); });