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:
shumeiko.it
2026-10-24 12:37:36 +02:00
parent 851502d347
commit ac23bbd7a6
27 changed files with 1426 additions and 922 deletions

View File

@@ -1,7 +1,7 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { createInProcessReranker, MindDB, type Reranker } from '@waggle/core';
import { createInProcessReranker, type Reranker } from '@waggle/core';
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';
vi.mock('@waggle/core', async (importOriginal) => ({
...await importOriginal<typeof import('@waggle/core')>(),
@@ -9,21 +9,20 @@ vi.mock('@waggle/core', async (importOriginal) => ({
}));
/**
* W4.2 — reranker wiring in recallMemory (W4-PRODUCTION-PORT-PLAN §5 W4.2).
* The orchestrator accepts an injected Reranker (tests) and otherwise
* lazy-creates one only behind WAGGLE_RERANKER=1 — flag-off default keeps
* CI free of the ~22MB model download.
* W4.2 — reranker wiring in recallMemory. The orchestrator accepts an injected
* Reranker (tests) and otherwise creates the in-process one lazily, only when
* WAGGLE_RERANKER=1. It is off by default: on the reference set it did not
* improve mem0's ranking (docs/oss/research/memory-search-quality.md).
*/
describe('W4.2 — recallMemory reranker wiring', () => {
let db: MindDB;
let memory: MemoryStore;
beforeEach(() => {
db = new MindDB(':memory:');
memory = createInMemoryMemoryStore();
});
afterEach(() => {
db.close();
vi.clearAllMocks();
vi.unstubAllEnvs();
});
@@ -36,82 +35,101 @@ describe('W4.2 — recallMemory reranker wiring', () => {
};
}
it('skips embedding and lazy reranker startup when both minds are empty', async () => {
async function seedWeeklyReport(): Promise<void> {
await memory.remember({ scope: PERSONAL_SCOPE, text: 'User preference: weekly report goes out on Fridays' });
await memory.remember({ scope: PERSONAL_SCOPE, text: 'User preference: weekly report wraps before standup' });
}
it('skips search and lazy reranker startup when memory is empty', async () => {
vi.stubEnv('WAGGLE_RERANKER', '1');
const embedder = new MockEmbedder();
const embedSpy = vi.spyOn(embedder, 'embed');
const embedBatchSpy = vi.spyOn(embedder, 'embedBatch');
const workspaceDb = new MindDB(':memory:');
const orchestrator = new Orchestrator({ db, embedder });
orchestrator.setWorkspaceMind(workspaceDb);
const search = vi.spyOn(memory, 'search');
const orchestrator = new Orchestrator({ memory });
orchestrator.setWorkspace('ws-1');
try {
const result = await orchestrator.recallMemory('weekly report');
const result = await orchestrator.recallMemory('weekly report');
expect(result).toEqual({ text: '', count: 0, recalled: [], recalledFrames: [] });
expect(createInProcessReranker).not.toHaveBeenCalled();
expect(embedSpy).not.toHaveBeenCalled();
expect(embedBatchSpy).not.toHaveBeenCalled();
} finally {
workspaceDb.close();
}
expect(result).toEqual({ text: '', count: 0, recalled: [], recalledFrames: [] });
expect(createInProcessReranker).not.toHaveBeenCalled();
expect(search).not.toHaveBeenCalled();
});
it('uses an injected reranker to order recall results', async () => {
const calls = { n: 0 };
const orchestrator = new Orchestrator({
db,
embedder: new MockEmbedder(),
reranker: markerReranker('Fridays', calls),
});
await orchestrator.executeTool('save_memory', {
content: 'User preference: weekly report goes out on Fridays',
importance: 'normal',
});
await orchestrator.executeTool('save_memory', {
content: 'User preference: weekly report wraps before standup',
importance: 'normal',
});
await seedWeeklyReport();
const orchestrator = new Orchestrator({ memory, reranker: markerReranker('standup', calls) });
const result = await orchestrator.recallMemory('weekly report');
expect(result.count).toBeGreaterThan(0);
const result = await orchestrator.recallMemory('weekly report Fridays');
expect(calls.n).toBeGreaterThan(0); // reranker actually consulted
const fridayIdx = result.text.indexOf('Fridays');
// The store ranks the Fridays memory first (more shared words); the reranker wins.
const standupIdx = result.text.indexOf('standup');
const fridayIdx = result.text.indexOf('goes out on Fridays');
expect(standupIdx).toBeGreaterThan(-1);
expect(fridayIdx).toBeGreaterThan(-1);
if (standupIdx > -1) expect(fridayIdx).toBeLessThan(standupIdx);
expect(standupIdx).toBeLessThan(fridayIdx);
});
it('does not create a reranker when the kill switch is set and none injected', async () => {
process.env['WAGGLE_RERANKER'] = '0'; // kill switch (also pinned in vitest.setup)
const orchestrator = new Orchestrator({ db, embedder: new MockEmbedder() });
await orchestrator.executeTool('save_memory', {
content: 'User preference: weekly report goes out on Fridays',
importance: 'normal',
});
// Must not attempt the ~22MB model load — recall just works RRF-only.
it('reranks a wider pool than the requested limit', async () => {
await seedWeeklyReport();
const orchestrator = new Orchestrator({ memory, reranker: markerReranker('standup', { n: 0 }) });
// With limit 1 the store alone would return only the Fridays memory.
const result = await orchestrator.recallMemory('weekly report Fridays', 1);
expect(result.text).toContain('standup');
expect(result.text).not.toContain('goes out on Fridays');
});
it('keeps the store ordering when the reranker throws', async () => {
await seedWeeklyReport();
const failing: Reranker = {
score: async () => { throw new Error('model crashed'); },
scoreBatch: async () => { throw new Error('model crashed'); },
};
const orchestrator = new Orchestrator({ memory, reranker: failing });
const result = await orchestrator.recallMemory('weekly report Fridays');
expect(result.count).toBe(2);
expect(result.text.indexOf('goes out on Fridays')).toBeLessThan(result.text.indexOf('standup'));
});
it.each([
['unset', undefined],
['the kill switch', '0'],
['a value other than 1', 'true'],
])('does not create a reranker when WAGGLE_RERANKER is %s', async (_label, value) => {
vi.stubEnv('WAGGLE_RERANKER', value);
await seedWeeklyReport();
const orchestrator = new Orchestrator({ memory });
const result = await orchestrator.recallMemory('weekly report');
expect(result.count).toBeGreaterThan(0);
expect(result.count).toBe(2);
expect(createInProcessReranker).not.toHaveBeenCalled();
});
it('passes the managed cache directory to the lazy reranker factory', async () => {
it('creates the lazy reranker once, with the managed cache directory, when WAGGLE_RERANKER=1', async () => {
vi.stubEnv('WAGGLE_RERANKER', '1');
const calls = { n: 0 };
vi.mocked(createInProcessReranker).mockResolvedValueOnce(markerReranker('Fridays', calls));
const rerankerCacheDir = 'C:\\Waggle\\models\\reranker';
const orchestrator = new Orchestrator({
db,
embedder: new MockEmbedder(),
rerankerCacheDir,
});
await orchestrator.executeTool('save_memory', {
content: 'User preference: weekly report goes out on Fridays',
importance: 'normal',
});
const orchestrator = new Orchestrator({ memory, rerankerCacheDir });
await seedWeeklyReport();
await orchestrator.recallMemory('weekly report');
await orchestrator.recallMemory('weekly report');
expect(createInProcessReranker).toHaveBeenCalledTimes(1);
expect(createInProcessReranker).toHaveBeenCalledWith({ cacheDir: rerankerCacheDir });
expect(calls.n).toBeGreaterThan(0);
});
it('falls back to the store ordering when the lazy reranker cannot be created', async () => {
vi.stubEnv('WAGGLE_RERANKER', '1');
vi.mocked(createInProcessReranker).mockRejectedValueOnce(new Error('offline'));
await seedWeeklyReport();
const orchestrator = new Orchestrator({ memory });
const result = await orchestrator.recallMemory('weekly report');
expect(result.count).toBe(2);
expect(result.text).not.toContain('temporarily unavailable');
});
});