diff --git a/mem0-ts/src/oss/src/memory/index.ts b/mem0-ts/src/oss/src/memory/index.ts index 71a10758d..b371d3a27 100644 --- a/mem0-ts/src/oss/src/memory/index.ts +++ b/mem0-ts/src/oss/src/memory/index.ts @@ -195,7 +195,10 @@ export class Memory { ...this.config.vectorStore.config, collectionName: entityCollectionName, }; - // For file-based stores (memory/SQLite), use a separate DB path for entities + // For file-based stores (memory/SQLite), use a separate DB path for entities. + // If dbPath was set explicitly, derive the entity path from it. If it's unset, + // leave it unset — the vector store's default-path logic now scopes by + // collectionName, so the entity store will land in its own file automatically. if (entityConfig.dbPath) { entityConfig.dbPath = entityConfig.dbPath.replace( /\.db$/, diff --git a/mem0-ts/src/oss/src/utils/sqlite.ts b/mem0-ts/src/oss/src/utils/sqlite.ts index 7da81c6c8..d752249f8 100644 --- a/mem0-ts/src/oss/src/utils/sqlite.ts +++ b/mem0-ts/src/oss/src/utils/sqlite.ts @@ -2,8 +2,16 @@ import fs from "fs"; import os from "os"; import path from "path"; -export function getDefaultVectorStoreDbPath(): string { - return path.join(os.homedir(), ".mem0", "vector_store.db"); +export function getDefaultVectorStoreDbPath(collectionName?: string): string { + // Scope the default DB file by collection name so that parallel stores + // (e.g. "memories" vs "memories_entities") don't collide in the same + // SQLite table. Without this, both collections write to the same + // `vectors` table and search results leak across them. + const filename = + collectionName && collectionName.length > 0 + ? `vector_store_${collectionName.replace(/[^a-zA-Z0-9_-]/g, "_")}.db` + : "vector_store.db"; + return path.join(os.homedir(), ".mem0", filename); } export function ensureSQLiteDirectory(dbPath: string): void { diff --git a/mem0-ts/src/oss/src/vector_stores/memory.ts b/mem0-ts/src/oss/src/vector_stores/memory.ts index 1a4f2ec21..6addf503f 100644 --- a/mem0-ts/src/oss/src/vector_stores/memory.ts +++ b/mem0-ts/src/oss/src/vector_stores/memory.ts @@ -21,7 +21,10 @@ export class MemoryVectorStore implements VectorStore { constructor(config: VectorStoreConfig) { this.dimension = config.dimension || 1536; // Default OpenAI dimension - this.dbPath = config.dbPath || getDefaultVectorStoreDbPath(); + // Scope the default path by collectionName so that parallel stores + // (e.g. memory vs entity) don't share the same `vectors` table. + this.dbPath = + config.dbPath || getDefaultVectorStoreDbPath(config.collectionName); if (!config.dbPath) { const oldDefault = path.join(process.cwd(), "vector_store.db");