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

@@ -50,7 +50,8 @@ export interface TextEmbedder {
export interface ExtractionModel {
readonly baseURL: string;
readonly apiKey: string;
readonly model: string;
/** The model name, or a function read on every extraction so a model switch applies at once. */
readonly model: string | (() => string);
}
export interface Mem0MemoryStoreOptions {
@@ -77,6 +78,7 @@ export async function createMem0MemoryStore(options: Mem0MemoryStoreOptions): Pr
const { Memory: Mem0 } = await import('mem0ai/oss');
const model = options.extractionModel;
const modelName = (): string => (typeof model?.model === 'function' ? model.model() : model?.model ?? '');
const mem0: Memory = new Mem0({
version: 'v1.1',
embedder: { provider: 'langchain', config: { model: asLangchainEmbeddings(options.embedder) } },
@@ -92,7 +94,7 @@ export async function createMem0MemoryStore(options: Mem0MemoryStoreOptions): Pr
llm: {
provider: 'openai',
config: model
? { baseURL: model.baseURL, apiKey: model.apiKey, model: model.model }
? { baseURL: model.baseURL, apiKey: model.apiKey, model: modelName() }
: { baseURL: 'http://127.0.0.1:9/v1', apiKey: 'unused', model: 'unused' },
},
historyStore: {
@@ -101,6 +103,14 @@ export async function createMem0MemoryStore(options: Mem0MemoryStoreOptions): Pr
},
});
if (typeof model?.model === 'function') {
// mem0 3.3.1 reads its LLM's `model` field on every call; a getter makes it
// follow the caller's current model. Re-check on any mem0 upgrade (ADR-0007).
const llm = (mem0 as unknown as { llm?: object }).llm;
if (!llm) throw new Error('mem0 changed its internals: no llm to configure');
Object.defineProperty(llm, 'model', { get: modelName, configurable: true });
}
const capabilities: MemoryCapabilities = {
extraction: model !== undefined,
semanticSearch: true,
@@ -343,3 +353,26 @@ function asLangchainEmbeddings(embedder: TextEmbedder) {
(await embedder.embedBatch(texts)).map((vector) => Array.from(vector)),
};
}
/**
* A deterministic bag-of-words embedder: texts that share words get similar
* vectors. It needs no model and no network, so tests and offline development
* use it; it does not understand meaning beyond shared words.
*/
export function createWordEmbedder(dimensions = 256): TextEmbedder {
const embed = async (text: string): Promise<number[]> => {
const vector = new Array<number>(dimensions).fill(0);
for (const word of text.toLowerCase().match(/[\p{L}\p{N}]{2,}/gu) ?? []) {
let hash = 2166136261;
for (const char of word) hash = Math.imul(hash ^ char.charCodeAt(0), 16777619);
vector[Math.abs(hash) % dimensions] += 1;
}
const norm = Math.hypot(...vector) || 1;
return vector.map((value) => value / norm);
};
return {
dimensions,
embed,
embedBatch: (texts) => Promise.all(texts.map(embed)),
};
}