Files
waggle-os/packages/agent/tests/orchestrator-context-frames.test.ts
shumeiko.it ac23bbd7a6 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.
2026-10-24 13:19:06 +02:00

119 lines
5.3 KiB
TypeScript

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';
/**
* Tests the PromptAssembler wiring points on Orchestrator:
* - `loadRecentContextFrames()`: typed counterpart to loadRecentContext
* - `recallMemory(query, limit, opts?)`: scoreFloor / tier options
*/
describe('Orchestrator PromptAssembler wiring', () => {
let memory: MemoryStore;
let orchestrator: Orchestrator;
beforeEach(() => {
memory = createInMemoryMemoryStore();
orchestrator = new Orchestrator({ memory });
});
describe('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([]);
expect(frames.activeWork).toEqual([]);
expect(frames.keyEntities).toEqual([]);
expect(frames.personalPreferences).toEqual([]);
});
it('populates activeWork from AwarenessLayer items', async () => {
orchestrator.getAwareness().add('task', 'Ship PromptAssembler', 10);
orchestrator.getAwareness().add('pending', 'Verify OpenRouter slugs', 5);
const frames = await orchestrator.loadRecentContextFrames();
expect(frames.activeWork).toHaveLength(2);
expect(frames.activeWork[0].content).toMatch(/Ship PromptAssembler|Verify OpenRouter/);
expect(frames.activeWork[0].priority).toBeGreaterThanOrEqual(0);
expect(['task', 'pending', 'action', 'flag']).toContain(frames.activeWork[0].category);
});
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 = await orchestrator.loadRecentContextFrames();
expect(frames.personalPreferences).toEqual(['User preference: prefers concise replies']);
});
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 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('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++) {
await memory.remember({ scope: PERSONAL_SCOPE, text: `State memory ${i}` });
}
const ctx = await orchestrator.loadRecentContextFrames(3);
expect(ctx.stateFrames.length + ctx.recentChanges.length).toBe(3);
});
});
describe('recallMemory() without options', () => {
it('returns empty result structure on empty memory', async () => {
const result = await orchestrator.recallMemory('anything');
expect(result).toEqual({ text: '', count: 0, recalled: [], recalledFrames: [] });
});
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');
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('scoreFloor drops search hits below the floor', async () => {
await memory.remember({ scope: PERSONAL_SCOPE, text: 'test memory about onboarding' });
// 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.count).toBe(0);
});
});
});