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,35 +1,24 @@
|
||||
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, type ContextFrames } from '../src/orchestrator.js';
|
||||
import { MockEmbedder } from '../../hive-mind-core/tests/mind/helpers/mock-embedder.js';
|
||||
|
||||
/**
|
||||
* Tests the PromptAssembler wiring points added to Orchestrator:
|
||||
* Tests the PromptAssembler wiring points on Orchestrator:
|
||||
* - `loadRecentContextFrames()`: typed counterpart to loadRecentContext
|
||||
* - `recallMemory(query, limit, opts?)`: extended with profile / scoreFloor / tier
|
||||
*
|
||||
* Byte-identical-when-absent contract: recallMemory must behave exactly as
|
||||
* the pre-extension version when opts is omitted.
|
||||
* - `recallMemory(query, limit, opts?)`: scoreFloor / tier options
|
||||
*/
|
||||
describe('Orchestrator PromptAssembler wiring', () => {
|
||||
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('loadRecentContextFrames()', () => {
|
||||
it('returns well-formed ContextFrames on an empty mind', () => {
|
||||
const frames: ContextFrames = orchestrator.loadRecentContextFrames();
|
||||
it('returns well-formed ContextFrames on an empty memory', async () => {
|
||||
const frames: ContextFrames = await orchestrator.loadRecentContextFrames();
|
||||
|
||||
expect(frames.stateFrames).toEqual([]);
|
||||
expect(frames.recentChanges).toEqual([]);
|
||||
@@ -38,11 +27,11 @@ describe('Orchestrator PromptAssembler wiring', () => {
|
||||
expect(frames.personalPreferences).toEqual([]);
|
||||
});
|
||||
|
||||
it('populates activeWork from AwarenessLayer items', () => {
|
||||
it('populates activeWork from AwarenessLayer items', async () => {
|
||||
orchestrator.getAwareness().add('task', 'Ship PromptAssembler', 10);
|
||||
orchestrator.getAwareness().add('pending', 'Verify OpenRouter slugs', 5);
|
||||
|
||||
const frames = orchestrator.loadRecentContextFrames();
|
||||
const frames = await orchestrator.loadRecentContextFrames();
|
||||
|
||||
expect(frames.activeWork).toHaveLength(2);
|
||||
expect(frames.activeWork[0].content).toMatch(/Ship PromptAssembler|Verify OpenRouter/);
|
||||
@@ -53,86 +42,77 @@ describe('Orchestrator PromptAssembler wiring', () => {
|
||||
it('pulls personal preferences even when no workspace is active', async () => {
|
||||
await orchestrator.executeTool('save_memory', {
|
||||
content: 'User preference: prefers concise replies',
|
||||
kind: 'preference',
|
||||
importance: 'normal',
|
||||
});
|
||||
|
||||
const frames = orchestrator.loadRecentContextFrames();
|
||||
expect(frames.personalPreferences.some(p => p.includes('User preference'))).toBe(true);
|
||||
const frames = await orchestrator.loadRecentContextFrames();
|
||||
expect(frames.personalPreferences).toEqual(['User preference: prefers concise replies']);
|
||||
});
|
||||
|
||||
it('splits frames by frame_type: I → stateFrames, P/B → recentChanges', () => {
|
||||
const frames = orchestrator.getFrames();
|
||||
const session = orchestrator.getSessions().ensureActive();
|
||||
const iFrame = frames.createIFrame(session.gop_id, 'Identity snapshot', 'important');
|
||||
frames.createPFrame(session.gop_id, 'Delta update', iFrame.id, 'normal');
|
||||
it('pulls personal preferences while a workspace is active', async () => {
|
||||
await orchestrator.executeTool('save_memory', {
|
||||
content: 'User preference: prefers concise replies',
|
||||
kind: 'preference',
|
||||
});
|
||||
orchestrator.setWorkspace('ws-1');
|
||||
|
||||
const ctx = orchestrator.loadRecentContextFrames();
|
||||
|
||||
expect(ctx.stateFrames.length).toBeGreaterThanOrEqual(1);
|
||||
expect(ctx.stateFrames.every(f => f.frame_type === 'I')).toBe(true);
|
||||
expect(ctx.recentChanges.some(f => f.frame_type === 'P')).toBe(true);
|
||||
const frames = await orchestrator.loadRecentContextFrames();
|
||||
expect(frames.personalPreferences).toEqual(['User preference: prefers concise replies']);
|
||||
// The personal preference is not workspace memory.
|
||||
expect([...frames.stateFrames, ...frames.recentChanges]).toEqual([]);
|
||||
});
|
||||
|
||||
it('respects the limit argument', () => {
|
||||
const frames = orchestrator.getFrames();
|
||||
const session = orchestrator.getSessions().ensureActive();
|
||||
it('splits by importance: critical and important → stateFrames, the rest → recentChanges', async () => {
|
||||
await memory.remember({ scope: PERSONAL_SCOPE, text: 'Launch date is fixed', importance: 'critical' });
|
||||
await memory.remember({ scope: PERSONAL_SCOPE, text: 'Budget owner is Ana', importance: 'important' });
|
||||
await memory.remember({ scope: PERSONAL_SCOPE, text: 'Standup moved to 10am', importance: 'normal' });
|
||||
|
||||
const ctx = await orchestrator.loadRecentContextFrames();
|
||||
|
||||
expect(ctx.stateFrames.map(f => f.content)).toEqual(['Launch date is fixed', 'Budget owner is Ana']);
|
||||
expect(ctx.recentChanges.map(f => f.content)).toEqual(['Standup moved to 10am']);
|
||||
});
|
||||
|
||||
it('respects the limit argument', async () => {
|
||||
for (let i = 0; i < 20; i++) {
|
||||
frames.createIFrame(session.gop_id, `State frame ${i}`, 'normal');
|
||||
await memory.remember({ scope: PERSONAL_SCOPE, text: `State memory ${i}` });
|
||||
}
|
||||
|
||||
const ctx = orchestrator.loadRecentContextFrames(3);
|
||||
const total = ctx.stateFrames.length + ctx.recentChanges.length;
|
||||
expect(total).toBeLessThanOrEqual(3);
|
||||
const ctx = await orchestrator.loadRecentContextFrames(3);
|
||||
expect(ctx.stateFrames.length + ctx.recentChanges.length).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('recallMemory() byte-identical when opts absent', () => {
|
||||
it('returns empty result structure on empty mind', async () => {
|
||||
describe('recallMemory() without options', () => {
|
||||
it('returns empty result structure on empty memory', async () => {
|
||||
const result = await orchestrator.recallMemory('anything');
|
||||
expect(result.count).toBe(0);
|
||||
expect(result.text).toBe('');
|
||||
expect(result.recalled).toEqual([]);
|
||||
expect(result).toEqual({ text: '', count: 0, recalled: [], recalledFrames: [] });
|
||||
});
|
||||
|
||||
it('default-path shape unchanged (text starts with # Recalled Memories when hits exist)', async () => {
|
||||
const frames = orchestrator.getFrames();
|
||||
const session = orchestrator.getSessions().ensureActive();
|
||||
frames.createIFrame(session.gop_id, 'The KVARK license boundary is non-negotiable', 'important');
|
||||
|
||||
// Seed the search index
|
||||
await orchestrator.getSearch().indexFrame(
|
||||
(await new Promise<number>(resolve => {
|
||||
const raw = db.getDatabase();
|
||||
const row = raw.prepare('SELECT id FROM memory_frames ORDER BY id DESC LIMIT 1').get() as { id: number };
|
||||
resolve(row.id);
|
||||
})),
|
||||
'The KVARK license boundary is non-negotiable',
|
||||
);
|
||||
it('renders a "# Recalled Memories" block when there are hits', async () => {
|
||||
await memory.remember({ scope: PERSONAL_SCOPE, text: 'The license boundary is non-negotiable', importance: 'important' });
|
||||
|
||||
const result = await orchestrator.recallMemory('license boundary');
|
||||
// Either hit or miss — test just checks shape stability
|
||||
expect(typeof result.text).toBe('string');
|
||||
expect(typeof result.count).toBe('number');
|
||||
expect(Array.isArray(result.recalled)).toBe(true);
|
||||
expect(result.text.startsWith('# Recalled Memories\n')).toBe(true);
|
||||
expect(result.text).toContain('The license boundary is non-negotiable');
|
||||
expect(result.count).toBe(1);
|
||||
expect(result.recalled).toEqual(['The license boundary is non-negotiable']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('recallMemory() with RecallOptions', () => {
|
||||
it('accepts profile override without type errors', async () => {
|
||||
const result = await orchestrator.recallMemory('test', 5, { profile: 'recent' });
|
||||
expect(result).toBeDefined();
|
||||
expect(typeof result.count).toBe('number');
|
||||
});
|
||||
it('scoreFloor drops search hits below the floor', async () => {
|
||||
await memory.remember({ scope: PERSONAL_SCOPE, text: 'test memory about onboarding' });
|
||||
|
||||
it('accepts scoreFloor (filters when provided)', async () => {
|
||||
const result = await orchestrator.recallMemory('test', 5, { scoreFloor: 0.99 });
|
||||
// With a very high score floor and no seeded matches, count should be 0
|
||||
expect(result.count).toBe(0);
|
||||
// Word-overlap score of "test" against the memory is about 0.5.
|
||||
expect((await orchestrator.recallMemory('test', 5)).count).toBe(1);
|
||||
expect((await orchestrator.recallMemory('test', 5, { scoreFloor: 0.99 })).count).toBe(0);
|
||||
});
|
||||
|
||||
it('accepts tier hint (recorded, not acted upon in recallMemory)', async () => {
|
||||
const result = await orchestrator.recallMemory('test', 5, { tier: 'small' });
|
||||
expect(result).toBeDefined();
|
||||
expect(result.count).toBe(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user