refactor(memory): run agent recall, memory tools and the server on the memory contract (P1.5)

- Orchestrator, memory tools, context loader and pattern write-back use a
  MemoryStore and a workspace scope instead of MindDB layers; the knowledge
  graph tools, cognify, memory linker and feedback handler are gone (graph
  hidden in v1, ADR-0002).
- The server opens one mem0 store in <dataDir>/memory/, embedded by the
  in-process model on first use, and extracts facts about the user in the
  background with the model the user chose (memoryExtraction setting).
- Legacy .mind memories are copied into the store once, in the background;
  the legacy files are only read.
- Weaver consolidation, harvest, wiki, memory import, memory lanes, vector
  backfill and index reconciliation are removed; agent runs, loops, sub-agent
  results, connector fetch and the failed-turn capture write through the
  contract. Workspace sessions no longer hold a mind handle.
- FileIndexer and the install audit move to waggle.db; recall date helpers and
  the reranker are copied into core.
- The CLI REPL opens the same memory store.
This commit is contained in:
shumeiko.it
2026-10-24 11:56:26 +02:00
parent f3eb6b6652
commit 784ca770b1
86 changed files with 2520 additions and 8444 deletions

View File

@@ -1,79 +0,0 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import {
MindDB,
FrameStore,
SessionStore,
KnowledgeGraph,
HybridSearch,
} from '@waggle/core';
import { MockEmbedder } from '../../hive-mind-core/tests/mind/helpers/mock-embedder.js';
import { CognifyPipeline } from '../src/cognify.js';
describe('CognifyPipeline with linking', () => {
let db: MindDB;
let frames: FrameStore;
let sessions: SessionStore;
let knowledge: KnowledgeGraph;
let search: HybridSearch;
beforeEach(() => {
db = new MindDB(':memory:');
const embedder = new MockEmbedder();
frames = new FrameStore(db);
sessions = new SessionStore(db);
knowledge = new KnowledgeGraph(db);
search = new HybridSearch(db, embedder);
});
afterEach(() => {
db.close();
});
it('returns relatedFrames when enableLinking is true', async () => {
// First, index an existing frame so there's something to link to
const pipeline1 = new CognifyPipeline({
frames,
sessions,
knowledge,
search,
});
await pipeline1.cognify('PostgreSQL database migration strategies and best practices');
// Now create a pipeline with linking enabled
const pipeline2 = new CognifyPipeline({
frames,
sessions,
knowledge,
search,
enableLinking: true,
});
const result = await pipeline2.cognify(
'Working on the PostgreSQL database migration for the backend service'
);
expect(result.frameId).toBeGreaterThan(0);
expect(result.relatedFrames).toBeDefined();
expect(Array.isArray(result.relatedFrames)).toBe(true);
// Should find the previously indexed frame as related
if (result.relatedFrames && result.relatedFrames.length > 0) {
expect(result.relatedFrames[0]).toHaveProperty('frameId');
expect(result.relatedFrames[0]).toHaveProperty('content');
expect(result.relatedFrames[0]).toHaveProperty('score');
}
});
it('does not return relatedFrames when enableLinking is false', async () => {
const pipeline = new CognifyPipeline({
frames,
sessions,
knowledge,
search,
});
const result = await pipeline.cognify('Some content about TypeScript');
expect(result.relatedFrames).toBeUndefined();
});
});

View File

@@ -1,110 +0,0 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import {
MindDB,
FrameStore,
SessionStore,
KnowledgeGraph,
HybridSearch,
} from '@waggle/core';
import { MockEmbedder } from '../../hive-mind-core/tests/mind/helpers/mock-embedder.js';
import { CognifyPipeline } from '../src/cognify.js';
describe('CognifyPipeline', () => {
let db: MindDB;
let frames: FrameStore;
let sessions: SessionStore;
let knowledge: KnowledgeGraph;
let search: HybridSearch;
let pipeline: CognifyPipeline;
beforeEach(() => {
db = new MindDB(':memory:');
const embedder = new MockEmbedder();
frames = new FrameStore(db);
sessions = new SessionStore(db);
knowledge = new KnowledgeGraph(db);
search = new HybridSearch(db, embedder);
pipeline = new CognifyPipeline({
frames,
sessions,
knowledge,
search,
});
});
afterEach(() => {
db.close();
});
it('saves memory and extracts entities into knowledge graph', async () => {
const result = await pipeline.cognify(
'Had a meeting with Alice Johnson about migrating from PostgreSQL to SQLite for the waggle project.'
);
// Should have created a frame
expect(result.frameId).toBeGreaterThan(0);
// Should have extracted entities
expect(result.entitiesExtracted).toBeGreaterThanOrEqual(2);
// Should have created co-occurrence relations
expect(result.relationsCreated).toBeGreaterThanOrEqual(1);
// Verify entities are in the knowledge graph
const people = knowledge.getEntitiesByType('person');
const techs = knowledge.getEntitiesByType('technology');
expect(people.length + techs.length).toBeGreaterThanOrEqual(2);
});
it('indexes the frame for vector search', async () => {
const result = await pipeline.cognify(
'Working on the PostgreSQL database migration for the backend service.'
);
expect(result.frameId).toBeGreaterThan(0);
// Should be searchable via hybrid search
const searchResults = await search.search('database migration');
expect(searchResults.length).toBeGreaterThanOrEqual(1);
expect(searchResults[0].frame.id).toBe(result.frameId);
});
it('creates P-frame when I-frame already exists for the session', async () => {
// First cognify creates an I-frame
const first = await pipeline.cognify('Initial memory about TypeScript.');
const firstFrame = frames.getById(first.frameId);
expect(firstFrame?.frame_type).toBe('I');
// Second cognify in same session creates a P-frame
const second = await pipeline.cognify('Follow-up about React integration.');
const secondFrame = frames.getById(second.frameId);
expect(secondFrame?.frame_type).toBe('P');
});
it('respects importance parameter', async () => {
const result = await pipeline.cognify(
'Critical finding about security vulnerability in the API.',
'critical'
);
const frame = frames.getById(result.frameId);
expect(frame?.importance).toBe('critical');
});
it('uses provided gopId when given', async () => {
const session = sessions.create();
const result = await pipeline.cognify(
'Memory tied to a specific session with Docker.',
'normal',
session.gop_id
);
const frame = frames.getById(result.frameId);
expect(frame?.gop_id).toBe(session.gop_id);
});
it('does not create duplicate entities for repeated mentions', async () => {
await pipeline.cognify('Learning about PostgreSQL and PostgreSQL performance tuning.');
const techs = knowledge.getEntitiesByType('technology');
const pgEntities = techs.filter(e => e.name.toLowerCase().includes('postgresql'));
expect(pgEntities).toHaveLength(1);
});
});

View File

@@ -1,75 +0,0 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { MindDB, KnowledgeGraph } from '@waggle/core';
import { FeedbackHandler } from '../src/feedback-handler.js';
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as os from 'node:os';
describe('FeedbackHandler', () => {
let dbPath: string;
let db: MindDB;
let kg: KnowledgeGraph;
let handler: FeedbackHandler;
beforeEach(() => {
dbPath = path.join(fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-fb-')), 'test.mind');
db = new MindDB(dbPath);
kg = new KnowledgeGraph(db);
handler = new FeedbackHandler(kg);
});
afterEach(() => {
db.close();
fs.rmSync(path.dirname(dbPath), { recursive: true, force: true });
});
it('updates entity when user corrects a fact', () => {
const entity = kg.createEntity('person', 'Alice', { role: 'Engineer' });
handler.correctEntity(entity.id, { role: 'Senior Engineer' });
const updated = kg.getEntity(entity.id);
expect(updated).toBeDefined();
const props = JSON.parse(updated!.properties);
expect(props.role).toBe('Senior Engineer');
expect(props.last_corrected).toBeDefined();
});
it('preserves existing properties when correcting', () => {
const entity = kg.createEntity('person', 'Bob', { role: 'Dev', team: 'Alpha' });
handler.correctEntity(entity.id, { role: 'Lead Dev' });
const updated = kg.getEntity(entity.id);
const props = JSON.parse(updated!.properties);
expect(props.role).toBe('Lead Dev');
expect(props.team).toBe('Alpha');
});
it('does nothing when correcting non-existent entity', () => {
// Should not throw
handler.correctEntity(99999, { role: 'Ghost' });
});
it('invalidates entity by setting valid_to', () => {
const entity = kg.createEntity('fact', 'Sky is green', {});
handler.invalidateEntity(entity.id, 'User corrected: sky is blue');
const updated = kg.getEntity(entity.id);
expect(updated).toBeDefined();
expect(updated!.valid_to).not.toBeNull();
});
it('stores invalidation reason in properties', () => {
const entity = kg.createEntity('fact', 'Old fact', { source: 'guess' });
handler.invalidateEntity(entity.id, 'Proven wrong');
const updated = kg.getEntity(entity.id);
const props = JSON.parse(updated!.properties);
expect(props.invalidation_reason).toBe('Proven wrong');
expect(props.source).toBe('guess');
});
it('does nothing when invalidating non-existent entity', () => {
// Should not throw
handler.invalidateEntity(99999, 'Does not exist');
});
});

View File

@@ -1,59 +0,0 @@
import { describe, it, expect, vi } from 'vitest';
import type { HybridSearch, MemoryFrame } from '@waggle/core';
import { MemoryLinker } from '../src/memory-linker.js';
describe('MemoryLinker', () => {
const mockSearch = {
search: vi.fn(),
} as unknown as HybridSearch;
it('finds related frames above threshold', async () => {
const linker = new MemoryLinker({ search: mockSearch, threshold: 0.1 });
vi.mocked(mockSearch.search).mockResolvedValueOnce([
{
frame: { id: 1, content: 'TypeScript generics guide' } as unknown as MemoryFrame,
rrfScore: 0.5,
relevanceScore: 0.4,
finalScore: 0.45,
},
{
frame: { id: 2, content: 'Advanced type inference' } as unknown as MemoryFrame,
rrfScore: 0.3,
relevanceScore: 0.2,
finalScore: 0.25,
},
]);
const links = await linker.findRelated('TypeScript types');
expect(mockSearch.search).toHaveBeenCalledWith('TypeScript types', { limit: 5 });
expect(links).toEqual([
{ frameId: 1, content: 'TypeScript generics guide', score: 0.45 },
{ frameId: 2, content: 'Advanced type inference', score: 0.25 },
]);
});
it('filters out frames below threshold', async () => {
const linker = new MemoryLinker({ search: mockSearch, threshold: 0.3 });
vi.mocked(mockSearch.search).mockResolvedValueOnce([
{
frame: { id: 3, content: 'Cooking recipes' } as unknown as MemoryFrame,
rrfScore: 0.1,
relevanceScore: 0.05,
finalScore: 0.08,
},
{
frame: { id: 4, content: 'Grocery list' } as unknown as MemoryFrame,
rrfScore: 0.05,
relevanceScore: 0.02,
finalScore: 0.03,
},
]);
const links = await linker.findRelated('quantum physics');
expect(links).toEqual([]);
});
});