fix(memory): erase forgotten memories from mem0's history database

mem0's delete kept the deleted text in memory_history, and fact extraction
keeps the last messages of each scope in the same file. forget now purges
the memory's history rows, and forgetting a whole scope also drops the
conversation messages mem0 kept for it.
This commit is contained in:
shumeiko.it
2026-10-24 12:47:46 +02:00
parent 6688c1f027
commit 4a1c4c4c8e
4 changed files with 96 additions and 2 deletions

View File

@@ -148,6 +148,14 @@ export async function createMem0MemoryStore(options: Mem0MemoryStoreOptions): Pr
knowledgeGraph: false,
};
// Erasing a memory must erase its text. mem0's delete keeps the old text in
// its history table, and extraction keeps the last messages of each scope;
// both live in history.db, which mem0 3.3.1 holds open as `db.db`.
const historyDb = (mem0 as unknown as { db?: { db?: HistoryDatabase } }).db?.db;
if (!historyDb) throw new Error('mem0 changed its internals: no history database to purge');
const purgeHistory = historyDb.prepare('DELETE FROM memory_history WHERE memory_id = ?');
const purgeMessages = historyDb.prepare('DELETE FROM messages WHERE session_scope = ?');
let closed = false;
const ensureOpen = (): void => {
if (closed) throw new Error('memory store is closed');
@@ -269,13 +277,20 @@ export async function createMem0MemoryStore(options: Mem0MemoryStoreOptions): Pr
ensureOpen();
if (!(await getRecord(id))) return false;
await mem0.delete(id);
purgeHistory.run(id);
return true;
},
async forgetMatching(filter: MemoryFilter): Promise<number> {
ensureOpen();
const doomed = await matching(filter);
for (const record of doomed) await mem0.delete(record.id);
for (const record of doomed) {
await mem0.delete(record.id);
purgeHistory.run(record.id);
}
if (isWholeScope(filter)) {
for (const scope of filter.scopes) purgeMessages.run(sessionScope(scope));
}
return doomed.length;
},
@@ -316,6 +331,24 @@ function toHistoryEntry(row: HistoryRow): MemoryHistoryEntry | null {
return { event, previousText: row.previous_value ?? null, text: row.new_value ?? null, at };
}
/** The part of better-sqlite3 the store uses on mem0's history database. */
interface HistoryDatabase {
prepare(sql: string): { run(...params: unknown[]): unknown };
}
/** A filter that selects everything in its scopes. */
function isWholeScope(filter: MemoryFilter): boolean {
return !filter.kinds && !filter.minImportance && filter.origin === undefined
&& filter.sessionId === undefined && filter.since === undefined && filter.until === undefined;
}
/** mem0's key for the conversation messages it keeps per scope (agent_id, user_id, sorted). */
function sessionScope(scope: MemoryScope): string {
const escape = (value: string): string =>
value.replace(/%/g, '%25').replace(/&/g, '%26').replace(/=/g, '%3D');
return `agent_id=${escape(scopeKey(scope))}&user_id=${escape(USER_ID)}`;
}
function scopeFilter(scope: MemoryScope): { user_id: string; agent_id: string } {
return { user_id: USER_ID, agent_id: scopeKey(scope) };
}