import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; import { createInMemoryMemoryStore, PERSONAL_SCOPE, workspaceScope, type MemoryImportance, type MemoryKind, type MemoryScope, type MemoryStore, } from '@waggle/memory-contract'; import { Orchestrator } from '../src/orchestrator.js'; /** * W4.3b — profile and event lanes in recallMemory. What the model learned * about the user (kind 'profile') renders in full; events (kind 'event') * render chronologically, with a dedicated section for a period the query * names. Lane memories never render a second time as search snippets. */ describe('W4.3b — recallMemory profile and event lanes', () => { let memory: MemoryStore; let orchestrator: Orchestrator; beforeEach(() => { memory = createInMemoryMemoryStore(); orchestrator = new Orchestrator({ memory }); }); afterEach(() => { vi.useRealTimers(); }); async function seed( text: string, kind: MemoryKind, opts: { at?: string; scope?: MemoryScope; importance?: MemoryImportance } = {}, ): Promise { if (opts.at) { // A write cannot set its own creation time, so the clock is moved instead. vi.useFakeTimers({ toFake: ['Date'] }); vi.setSystemTime(new Date(opts.at)); } await memory.remember({ scope: opts.scope ?? PERSONAL_SCOPE, text, kind, importance: opts.importance ?? 'normal', source: 'system', }); vi.useRealTimers(); } async function seedLanes(): Promise { await seed('Ana is a designer based in Belgrade who plays Hollow Knight.', 'profile'); await seed('User preference: Ana prefers dark mode', 'preference'); await seed('Ana shipped the redesign', 'event', { at: '2026-06-02T09:00:00.000Z' }); await seed('Ana visited the dentist', 'event', { at: '2026-05-07T09:00:00.000Z' }); } it('renders profile and chronological event sections', async () => { await seedLanes(); const result = await orchestrator.recallMemory('what is happening'); const text = result.text; expect(result.count).toBe(3); expect(text).toContain('## Profiles\n- Ana is a designer based in Belgrade'); expect(text).toContain('## Events (chronological)'); expect(text).toContain('- [2026-05-07] Ana visited the dentist'); expect(text).toContain('- [2026-06-02] Ana shipped the redesign'); // profiles before events; events oldest first even though written newest first expect(text.indexOf('## Profiles')).toBeLessThan(text.indexOf('## Events (chronological)')); expect(text.indexOf('dentist')).toBeLessThan(text.indexOf('redesign')); }); it('explicit-period query gets a dedicated windowed events section', async () => { await seedLanes(); const result = await orchestrator.recallMemory('what did Ana do in May 2026?'); expect(result.text).toContain('## Events during may 2026'); const windowed = result.text.slice(result.text.indexOf('## Events during')); expect(windowed).toContain('dentist'); expect(windowed).not.toContain('redesign'); }); it('profile memories never double-render as search snippets', async () => { await seedLanes(); // The query shares words with the profile, so search finds it too. const result = await orchestrator.recallMemory('Hollow Knight designer Belgrade'); expect(result.text.split('Hollow Knight').length - 1).toBe(1); expect(result.text).not.toContain('## Personal Memory'); }); it('lane-only recall still returns content (no search hits required)', async () => { await seed('Ana is a designer based in Belgrade.', 'profile'); const result = await orchestrator.recallMemory('zebra quantum xylophone'); expect(result.count).toBe(1); expect(result.text).toContain('## Profiles'); expect(result.recalled).toEqual(['Ana is a designer based in Belgrade.']); }); it('leaves temporary profile and event memories out of the lanes', async () => { await seed('Ana might be moving to Lisbon', 'profile', { importance: 'temporary' }); await seed('Ana possibly cancelled the trip', 'event', { importance: 'temporary' }); const result = await orchestrator.recallMemory('zebra quantum xylophone'); expect(result.count).toBe(0); expect(result.text).toBe(''); }); it('renders the 40 most recent events', async () => { for (let day = 1; day <= 41; day++) { const at = new Date(Date.UTC(2026, 0, day, 9)).toISOString(); await seed(`Daily log entry ${day}`, 'event', { at }); } const result = await orchestrator.recallMemory('zebra quantum xylophone'); expect(result.text).not.toContain('Daily log entry 1\n'); expect(result.text).toContain('Daily log entry 2\n'); expect(result.text).toContain('Daily log entry 41'); expect(result.count).toBe(40); }); it('takes profiles from personal and workspace memory, events from the active scope', async () => { await seed('Ana is a designer based in Belgrade.', 'profile'); await seed('Ana leads the Atlas redesign.', 'profile', { scope: workspaceScope('ws-1') }); await seed('Atlas kickoff happened', 'event', { scope: workspaceScope('ws-1'), at: '2026-05-04T09:00:00.000Z' }); await seed('Ana adopted a cat', 'event', { at: '2026-05-05T09:00:00.000Z' }); orchestrator.setWorkspace('ws-1'); const result = await orchestrator.recallMemory('zebra quantum xylophone'); expect(result.text).toContain('Ana is a designer based in Belgrade.'); expect(result.text).toContain('Ana leads the Atlas redesign.'); expect(result.text).toContain('Atlas kickoff happened'); expect(result.text).not.toContain('Ana adopted a cat'); }); });