import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; import { TEMPORAL_GUIDANCE } from '@waggle/core'; import { createInMemoryMemoryStore, PERSONAL_SCOPE, workspaceScope, type MemoryStore } from '@waggle/memory-contract'; import { Orchestrator } from '../src/orchestrator.js'; /** * W4.1a — production port of the benchmark-proven temporal recall surface. * * #1 Date rendering + TEMPORAL_GUIDANCE: recallMemory's rendered block carries * a reference-date anchor line and the temporal guidance fragment. * #2 Unconditional importance lane: critical/important memories reach recall * on EVERY query, not only on catch-up regex matches. * #3 Date window: a query naming an explicit period restricts search to * memories created in it, and falls back to unwindowed search when the * window holds nothing. */ describe('W4.1a — temporal recall surface + unconditional importance lane', () => { let memory: MemoryStore; let orchestrator: Orchestrator; beforeEach(() => { memory = createInMemoryMemoryStore(); orchestrator = new Orchestrator({ memory }); }); afterEach(() => { vi.useRealTimers(); }); /** A write cannot set its own creation time, so the clock is moved instead. */ async function rememberAt(iso: string, text: string): Promise { vi.useFakeTimers({ toFake: ['Date'] }); vi.setSystemTime(new Date(iso)); await memory.remember({ scope: PERSONAL_SCOPE, text }); vi.useRealTimers(); } describe('#1 TEMPORAL_GUIDANCE wording', () => { it('carries the granularity clause and relative-date worked example', () => { expect(TEMPORAL_GUIDANCE).toMatch(/granularit/i); expect(TEMPORAL_GUIDANCE).toMatch(/confidently wrong exact day/i); expect(TEMPORAL_GUIDANCE).toMatch(/last year/i); expect(TEMPORAL_GUIDANCE).toMatch(/most recent version is correct/i); }); }); describe('#1 recallMemory renders anchor line + guidance', () => { it('includes the reference-date anchor and temporal guidance in the block', async () => { await orchestrator.executeTool('save_memory', { content: 'User preference: weekly report goes out on Fridays', importance: 'important', }); const result = await orchestrator.recallMemory('weekly report'); expect(result.count).toBeGreaterThan(0); expect(result.text).toContain('Reference date (most recent memory):'); // The guidance fragment rides with the recalled block (not the global prompt) expect(result.text).toContain('timestamped [YYYY-MM-DD]'); }); it('anchor date equals the most recent rendered memory date', async () => { await rememberAt('2026-03-02T09:00:00.000Z', 'Weekly report template was agreed'); await rememberAt('2026-04-15T09:00:00.000Z', 'Weekly report moved to Fridays'); const result = await orchestrator.recallMemory('weekly report'); expect(result.text).toContain('Reference date (most recent memory): 2026-04-15'); }); }); describe('#2 unconditional importance lane', () => { it('recalls a critical memory on a semantically unrelated query', async () => { // Disjoint vocabulary: search cannot surface it; only the importance lane can. await orchestrator.executeTool('save_memory', { content: 'Production launch is locked for June 20', importance: 'critical', }); const result = await orchestrator.recallMemory('favorite painting colors'); expect(result.text).toContain('Production launch is locked for June 20'); }); it('does not duplicate a memory surfaced by both semantic and importance lanes', async () => { await orchestrator.executeTool('save_memory', { content: 'Production launch is locked for June 20', importance: 'critical', }); const result = await orchestrator.recallMemory('production launch'); expect(result.text.split('Production launch is locked for June 20').length - 1).toBe(1); }); it('caps the lane at five memories, critical before important', async () => { for (let i = 1; i <= 6; i++) { await memory.remember({ scope: PERSONAL_SCOPE, text: `Important constraint number ${i}`, importance: 'important' }); } await memory.remember({ scope: PERSONAL_SCOPE, text: 'Critical constraint alpha', importance: 'critical' }); const result = await orchestrator.recallMemory('zebra xylophone'); expect(result.count).toBe(5); const lines = result.text.split('\n').filter(l => l.includes('constraint')); expect(lines[0]).toContain('Critical constraint alpha'); }); it('takes the lane from the active workspace only', async () => { await memory.remember({ scope: PERSONAL_SCOPE, text: 'Personal critical rule about tea', importance: 'critical' }); await memory.remember({ scope: workspaceScope('ws-1'), text: 'Workspace critical rule about deploys', importance: 'critical' }); orchestrator.setWorkspace('ws-1'); const result = await orchestrator.recallMemory('zebra xylophone'); expect(result.text).toContain('Workspace critical rule about deploys'); expect(result.text).not.toContain('Personal critical rule about tea'); }); it('importance lane respects the temporary exclusion', async () => { // temporary memories must never re-enter the prompt as authoritative recall (R2 sign-gate) await orchestrator.executeTool('save_memory', { content: 'Scratch note: half-finished thought', importance: 'temporary', }); const result = await orchestrator.recallMemory('favorite painting colors'); expect(result.text).not.toContain('Scratch note'); }); }); describe('#3 date window', () => { it('restricts search to memories created in the named period', async () => { await rememberAt('2026-05-12T10:00:00.000Z', 'Weekly report covered the hiring plan'); await rememberAt('2026-06-09T10:00:00.000Z', 'Weekly report covered the budget review'); const result = await orchestrator.recallMemory('what did the weekly report say in May 2026'); expect(result.text).toContain('hiring plan'); expect(result.text).not.toContain('budget review'); }); it('includes memories from the first and last day of the window', async () => { await rememberAt('2026-05-01T00:00:01.000Z', 'Weekly report opened the month'); await rememberAt('2026-05-31T23:59:59.000Z', 'Weekly report closed the month'); const result = await orchestrator.recallMemory('weekly report in May 2026'); expect(result.text).toContain('opened the month'); expect(result.text).toContain('closed the month'); }); it('falls back to unwindowed recall when the window holds nothing', async () => { await orchestrator.executeTool('save_memory', { content: 'User preference: weekly report goes out on Fridays', importance: 'normal', }); const result = await orchestrator.recallMemory('what did the weekly report say in May 2019'); expect(result.text).toContain('weekly report goes out on Fridays'); }); }); });