/** * R5 / R2 closure — the memory-loop sign gate must be closed *end to end*. * * R2 wired `isSelfIncapacityAssertion()` into the autoSave `save()` chokepoint * so DEFECT-2 poison is coerced to `temporary` importance. Its contract * (memory-sign-gate.ts) is that `temporary` memories are *recall-excluded*. * * Search ranks by relevance and does not filter on importance, so a * sign-gated memory is still retrievable by search; recallMemory must drop it * before it re-enters the prompt. This is the behavioral proof of the closed * loop, not just of the classifier. */ import { describe, it, expect, beforeEach } from 'vitest'; import { createInMemoryMemoryStore, PERSONAL_SCOPE, type MemoryStore } from '@waggle/memory-contract'; import { Orchestrator } from '../src/orchestrator.js'; describe('R2 sign-gate is closed end-to-end (temporary excluded from semantic recall)', () => { let memory: MemoryStore; let orchestrator: Orchestrator; beforeEach(() => { memory = createInMemoryMemoryStore(); orchestrator = new Orchestrator({ memory }); }); it('does not surface a sign-gated (temporary) DEFECT-2 memory via recallMemory, but still recalls the authoritative decision', async () => { // A self-incapacity assertion — the R2 save-side gate coerces this exact // class to `temporary` (orchestrator.ts:757). Seed it at the importance the // gate produces. await orchestrator.executeTool('save_memory', { content: "run npm install -g @modelcontextprotocol/server-filesystem yourself and restart the session before I can use it", importance: 'temporary', }); // The authoritative truth on the same topic — the control. Recall MUST // still surface this; the exclusion has to be specific to `temporary`. await orchestrator.executeTool('save_memory', { content: 'Decision: the MCP filesystem connector is installed and wired for this workspace', importance: 'important', }); // Precondition: seeds landed at the intended importances (fences off the // save_memory dramatic-claim downgrade / dedup confounds — if either fired // this fails here with a clear signal rather than passing vacuously). const records = await memory.list({ scopes: [PERSONAL_SCOPE] }); const poison = records.find(r => r.text.includes('npm install -g @modelcontextprotocol')); const decision = records.find(r => r.text.includes('connector is installed and wired')); expect(poison?.importance, 'poison memory must be stored as temporary').toBe('temporary'); expect(decision?.importance, 'decision memory must be stored as important').toBe('important'); // The search itself still finds the poison; only recall may drop it. const hits = await memory.search('how do I install the mcp filesystem connector', { scopes: [PERSONAL_SCOPE] }); expect(hits.some(h => h.text.includes('npm install -g @modelcontextprotocol'))).toBe(true); const result = await orchestrator.recallMemory( 'how do I install the mcp filesystem connector', ); // The closed-loop invariant: a sign-gated memory must NOT re-enter the // prompt as authoritative recall… expect(result.text).not.toContain('run npm install -g @modelcontextprotocol'); // …while legitimate, authoritative memory on the same query still surfaces. expect(result.text).toContain('connector is installed and wired'); }); });