test(agent): move the agent suites onto the memory contract (P1.5)
Rewrites the orchestrator, recall, compaction, save_memory and assembler suites for MemoryStore (createInMemoryMemoryStore; mem0 with the word embedder for the M3b integration test), string ids, async prompt building and setWorkspace(). Creation times are set with fake timers, since a write cannot set its own. Stores of Waggle's own data (identity, traces) are built on WaggleDB; the session-manager stubs use the new create() signature. Kept behaviour: importance lane (cap, active scope, temporary exclusion), profile and event lanes, date window and its fallback, catch-up, injection blocking of recall and preloaded context, autosave safety, decision consent, compaction update-in-place, reranker wiring. New tests: recall with a workspace active versus personal only; fetchRecentMemories ordering; toRecallFrame; context loader; save_memory routing, dedup, rate limit, dramatic-claim downgrade and contradiction scope; search_memory and search_all_workspaces; pattern write-back kinds, scopes and sign gate; read_other_workspace; reranker off unless WAGGLE_RERANKER=1, wider rerank pool, failure fallbacks. Deleted, because the behaviour is gone: - w46-rawdetail-recall.test.ts: the raw-turn (RAWDETAIL) recall lane. - orchestrator.test.ts, query_knowledge (3 tests) and the recall-hardening M4 "Key Knowledge" test: the knowledge graph is not in v1 (ADR-0002). - orchestrator-context-frames "profile override": scoring profiles. - Assertions on I/P frame types, GOP sessions, FTS rows, the "Memory Facts" lane, cognify stages and session/entity counts were replaced by the equivalent contract-level checks.
This commit is contained in:
@@ -1,34 +1,25 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import { MindDB } from '@waggle/core';
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import { createInMemoryMemoryStore, PERSONAL_SCOPE, type MemoryStore } from '@waggle/memory-contract';
|
||||
import { Orchestrator } from '../src/orchestrator.js';
|
||||
import { MockEmbedder } from '../../hive-mind-core/tests/mind/helpers/mock-embedder.js';
|
||||
|
||||
describe('Agent Loop with Custom Tools', () => {
|
||||
let db: MindDB;
|
||||
let memory: MemoryStore;
|
||||
let orchestrator: Orchestrator;
|
||||
|
||||
beforeEach(() => {
|
||||
db = new MindDB(':memory:');
|
||||
orchestrator = new Orchestrator({
|
||||
db,
|
||||
embedder: new MockEmbedder(),
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
db.close();
|
||||
memory = createInMemoryMemoryStore();
|
||||
orchestrator = new Orchestrator({ memory });
|
||||
});
|
||||
|
||||
describe('Tool definitions', () => {
|
||||
it('creates all required tool definitions', () => {
|
||||
const tools = orchestrator.getTools();
|
||||
const names = tools.map(t => t.name);
|
||||
expect(names).toContain('search_memory');
|
||||
expect(names).toContain('save_memory');
|
||||
expect(names).toContain('get_identity');
|
||||
expect(names).toContain('get_awareness');
|
||||
expect(names).toContain('query_knowledge');
|
||||
expect(names).toContain('add_task');
|
||||
const names = orchestrator.getTools().map(t => t.name);
|
||||
expect(names).toEqual(expect.arrayContaining([
|
||||
'search_memory', 'search_all_workspaces', 'save_memory', 'get_identity', 'get_awareness', 'add_task',
|
||||
]));
|
||||
// The knowledge graph is not part of v1 (ADR-0002).
|
||||
expect(names).not.toContain('query_knowledge');
|
||||
expect(names).not.toContain('correct_knowledge');
|
||||
});
|
||||
|
||||
it('each tool has name, description, parameters, and execute function', () => {
|
||||
@@ -76,7 +67,7 @@ describe('Agent Loop with Custom Tools', () => {
|
||||
});
|
||||
|
||||
describe('Tool execution: save_memory', () => {
|
||||
it('saves a memory to the current session', async () => {
|
||||
it('saves a memory', async () => {
|
||||
const result = await orchestrator.executeTool('save_memory', {
|
||||
content: 'User prefers dark mode',
|
||||
importance: 'normal',
|
||||
@@ -84,16 +75,21 @@ describe('Agent Loop with Custom Tools', () => {
|
||||
expect(result).toContain('Memory saved');
|
||||
});
|
||||
|
||||
it('creates a session if none active', async () => {
|
||||
it('writes to personal memory as an agent fact when no workspace is active', async () => {
|
||||
await orchestrator.executeTool('save_memory', { content: 'First memory' });
|
||||
const active = orchestrator.getSessions().getActive();
|
||||
expect(active.length).toBeGreaterThan(0);
|
||||
const [record] = await memory.list({ scopes: [PERSONAL_SCOPE] });
|
||||
expect(record).toMatchObject({
|
||||
text: 'First memory',
|
||||
scope: PERSONAL_SCOPE,
|
||||
kind: 'fact',
|
||||
importance: 'normal',
|
||||
source: 'user_stated',
|
||||
origin: 'agent',
|
||||
});
|
||||
});
|
||||
|
||||
it('saved memory is searchable', async () => {
|
||||
await orchestrator.executeTool('save_memory', { content: 'The meeting about quantum computing was postponed' });
|
||||
|
||||
// CognifyPipeline now auto-indexes frames for vector search
|
||||
const searchResult = await orchestrator.executeTool('search_memory', { query: 'quantum' });
|
||||
expect(searchResult).toContain('quantum computing');
|
||||
});
|
||||
@@ -105,41 +101,13 @@ describe('Agent Loop with Custom Tools', () => {
|
||||
expect(result).toContain('No relevant memories');
|
||||
});
|
||||
|
||||
it('searches with different profiles', async () => {
|
||||
it('honours the limit argument', async () => {
|
||||
await orchestrator.executeTool('save_memory', { content: 'Important meeting notes from today' });
|
||||
await orchestrator.executeTool('save_memory', { content: 'Meeting agenda for the planning review' });
|
||||
|
||||
// CognifyPipeline now auto-indexes frames for vector search
|
||||
const result = await orchestrator.executeTool('search_memory', {
|
||||
query: 'meeting',
|
||||
profile: 'recent',
|
||||
limit: 5,
|
||||
});
|
||||
expect(result).toContain('meeting notes');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Tool execution: query_knowledge', () => {
|
||||
it('queries entities from knowledge graph', async () => {
|
||||
orchestrator.getKnowledge().createEntity('person', 'Alice', { role: 'engineer' });
|
||||
orchestrator.getKnowledge().createEntity('person', 'Bob', { role: 'designer' });
|
||||
|
||||
const result = await orchestrator.executeTool('query_knowledge', { query: 'Alice' });
|
||||
expect(result).toContain('Alice');
|
||||
});
|
||||
|
||||
it('shows relations for found entities', async () => {
|
||||
const alice = orchestrator.getKnowledge().createEntity('person', 'Alice', {});
|
||||
const waggle = orchestrator.getKnowledge().createEntity('project', 'Waggle', {});
|
||||
orchestrator.getKnowledge().createRelation(alice.id, waggle.id, 'works_on', 0.95);
|
||||
|
||||
const result = await orchestrator.executeTool('query_knowledge', { query: 'Alice' });
|
||||
expect(result).toContain('works_on');
|
||||
expect(result).toContain('Waggle');
|
||||
});
|
||||
|
||||
it('returns empty message for no matches', async () => {
|
||||
const result = await orchestrator.executeTool('query_knowledge', { query: 'NonexistentPerson' });
|
||||
expect(result).toContain('No entities found');
|
||||
const result = await orchestrator.executeTool('search_memory', { query: 'meeting', limit: 1 });
|
||||
expect(result).toContain('[1]');
|
||||
expect(result).not.toContain('[2]');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -158,7 +126,7 @@ describe('Agent Loop with Custom Tools', () => {
|
||||
});
|
||||
|
||||
describe('System prompt builder', () => {
|
||||
it('builds system prompt from identity + awareness', () => {
|
||||
it('builds system prompt from identity + awareness', async () => {
|
||||
orchestrator.getIdentity().create({
|
||||
name: 'Waggle',
|
||||
role: 'Assistant',
|
||||
@@ -169,18 +137,25 @@ describe('Agent Loop with Custom Tools', () => {
|
||||
});
|
||||
orchestrator.getAwareness().add('task', 'Check emails', 5);
|
||||
|
||||
const prompt = orchestrator.buildSystemPrompt();
|
||||
const prompt = await orchestrator.buildSystemPrompt();
|
||||
expect(prompt).toContain('Waggle');
|
||||
expect(prompt).toContain('Check emails');
|
||||
// Self-awareness section present
|
||||
expect(prompt).toContain('# Self-Awareness');
|
||||
});
|
||||
|
||||
it('builds prompt without identity', () => {
|
||||
const prompt = orchestrator.buildSystemPrompt();
|
||||
it('builds prompt without identity', async () => {
|
||||
const prompt = await orchestrator.buildSystemPrompt();
|
||||
expect(prompt).toContain('# Self-Awareness');
|
||||
expect(prompt).not.toContain('# Identity');
|
||||
});
|
||||
|
||||
it('reports the number of readable memories', async () => {
|
||||
await orchestrator.executeTool('save_memory', { content: 'First fact about the launch plan' });
|
||||
await orchestrator.executeTool('save_memory', { content: 'Second fact about the hiring plan' });
|
||||
|
||||
const prompt = await orchestrator.buildSystemPrompt();
|
||||
expect(prompt).toContain('Memory: 2 memories.');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Unknown tool handling', () => {
|
||||
@@ -190,7 +165,7 @@ describe('Agent Loop with Custom Tools', () => {
|
||||
});
|
||||
|
||||
describe('E4: Auto-save style detection', () => {
|
||||
it('saves explicit user preferences to personal mind', async () => {
|
||||
it('saves explicit user preferences to personal memory', async () => {
|
||||
const saved = await orchestrator.autoSaveFromExchange(
|
||||
'I prefer bullet-point summaries for everything',
|
||||
'Got it! I will use bullet points going forward.'
|
||||
@@ -198,13 +173,12 @@ describe('Agent Loop with Custom Tools', () => {
|
||||
expect(saved.length).toBeGreaterThan(0);
|
||||
expect(saved[0]).toContain('User preference');
|
||||
|
||||
// Verify it was saved in memory
|
||||
const searchResult = await orchestrator.executeTool('search_memory', { query: 'preference' });
|
||||
expect(searchResult).toContain('bullet');
|
||||
});
|
||||
|
||||
it('detects implicit style signals from user messages', async () => {
|
||||
// Use phrasing that triggers implicit style detection, not explicit preference patterns
|
||||
// Phrasing that triggers implicit style detection, not explicit preference patterns
|
||||
const saved = await orchestrator.autoSaveFromExchange(
|
||||
'Can you present this data in a table format with columns for each category?',
|
||||
'Here is the table: ...'
|
||||
@@ -222,23 +196,39 @@ describe('Agent Loop with Custom Tools', () => {
|
||||
'Can you just show me the code for the other function too?',
|
||||
'Here is the other code: ...'
|
||||
);
|
||||
// Second save should not duplicate the style note
|
||||
const styleNotes = saved2.filter(s => s.includes('Style note'));
|
||||
expect(styleNotes.length).toBe(0);
|
||||
// The second detection is neither reported nor written again.
|
||||
expect(saved2.filter(s => s.includes('Style note'))).toHaveLength(0);
|
||||
const notes = (await memory.list({ scopes: [PERSONAL_SCOPE] })).filter(r => r.text.startsWith('Style note'));
|
||||
expect(notes).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('E4: Personal preferences in context', () => {
|
||||
it('includes personal preferences in loadRecentContext when workspace is active', async () => {
|
||||
// Save a preference first
|
||||
it('includes personal preferences in loadRecentContext without a workspace', async () => {
|
||||
await orchestrator.autoSaveFromExchange(
|
||||
'I always want concise answers from now on',
|
||||
'Understood, I will keep things concise.'
|
||||
);
|
||||
|
||||
// Without workspace, loadRecentContext just shows personal frames
|
||||
const ctx = orchestrator.loadRecentContext();
|
||||
expect(ctx).toContain('preference');
|
||||
const ctx = await orchestrator.loadRecentContext();
|
||||
expect(ctx).toContain('## Recent Personal Memory');
|
||||
expect(ctx).toContain('## Personal Preferences');
|
||||
expect(ctx).toContain('I always want concise answers');
|
||||
});
|
||||
|
||||
it('keeps personal preferences next to workspace memory when a workspace is active', async () => {
|
||||
await orchestrator.autoSaveFromExchange(
|
||||
'I always want concise answers from now on',
|
||||
'Understood, I will keep things concise.'
|
||||
);
|
||||
orchestrator.setWorkspace('ws-1');
|
||||
await orchestrator.executeTool('save_memory', { content: 'The launch review is on Thursday' });
|
||||
|
||||
const ctx = await orchestrator.loadRecentContext();
|
||||
expect(ctx).toContain('## Recent Workspace Memory');
|
||||
expect(ctx).toContain('The launch review is on Thursday');
|
||||
expect(ctx).toContain('## Personal Preferences (across all workspaces)');
|
||||
expect(ctx).toContain('I always want concise answers');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user