/** * Skills 2.0 gap K — write-path conflict detection. * * When save_memory writes a new memory that contradicts an existing one * (detectContradiction), the save still succeeds but the return string * carries a [flag: contradicts_existing …] marker. */ import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import { WaggleDB, IdentityLayer, AwarenessLayer } from '@waggle/core'; import { createInMemoryMemoryStore, PERSONAL_SCOPE, type MemoryStore } from '@waggle/memory-contract'; import { createMindTools } from '../src/tools.js'; import type { ToolDefinition } from '../src/tools.js'; describe('save_memory — gap K (write-path conflict detection)', () => { let waggleDb: WaggleDB; let memory: MemoryStore; let tools: ToolDefinition[]; function getSaveMemory(): ToolDefinition { const tool = tools.find(t => t.name === 'save_memory'); if (!tool) throw new Error('save_memory tool not found'); return tool; } beforeEach(() => { waggleDb = new WaggleDB(':memory:'); memory = createInMemoryMemoryStore(); tools = createMindTools({ memory, identity: new IdentityLayer(waggleDb), awareness: new AwarenessLayer(waggleDb), }); }); afterEach(() => { waggleDb.close(); }); it('flags contradictions and still saves the memory', async () => { const save = getSaveMemory(); // Prime with an affirmative decision. // Using "enable/disable feature flag rollout users" — the existing // detectContradiction algorithm needs asymmetric sentiment to fire; // inputs with "will" in both sides add positive noise that can mask // the polarity swing. Keep inputs tight and unambiguous. const first = await save.execute({ content: 'Decision: enable feature flag rollout for users; proceed and confirmed.', importance: 'important', target: 'personal', source: 'user_stated', }); expect(first).toContain('Memory saved'); expect(first).not.toContain('contradicts_existing'); // Contradicting decision — disable/reject/cancel instead of enable/approve const second = await save.execute({ content: 'Decision: disable feature flag rollout for users; cancel and reject.', importance: 'important', target: 'personal', source: 'user_stated', }); // Save still succeeded — conflict is a flag, not an abort expect(second).toContain('Memory saved'); // But the return message is annotated expect(second).toContain('[flag: contradicts_existing'); expect(await memory.count({ scopes: [PERSONAL_SCOPE] })).toBe(2); }); it('non-contradicting saves are not flagged', async () => { const save = getSaveMemory(); await save.execute({ content: 'Decision: we chose PostgreSQL for analytics; approved and confirmed.', target: 'personal', source: 'user_stated', }); const result = await save.execute({ content: 'Meeting notes from standup — discussed roadmap for Q2 and sprint planning.', target: 'personal', source: 'user_stated', }); expect(result).not.toContain('contradicts_existing'); }); });