Skip to content

fix(ts-sdk): default historyDbPath to ~/.mem0/history.db instead of r…#5007

Open
shafdev wants to merge 1 commit intomem0ai:mainfrom
shafdev:fix/history-db-default-path
Open

fix(ts-sdk): default historyDbPath to ~/.mem0/history.db instead of r…#5007
shafdev wants to merge 1 commit intomem0ai:mainfrom
shafdev:fix/history-db-default-path

Conversation

@shafdev
Copy link
Copy Markdown
Contributor

@shafdev shafdev commented Apr 28, 2026

Linked Issue

Closes #5004

Description

Default historyDbPath was set to "memory.db" (relative), causing
better-sqlite3 to create a fresh SQLite file in every CWD. Mirrors the
existing getDefaultVectorStoreDbPath() pattern — now defaults to
~/.mem0/history.db.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactor (no functional changes)
  • Documentation update

Breaking Changes

N/A

Test Coverage

  • I added/updated unit tests
  • I added/updated integration tests
  • I tested manually (describe below)
  • No tests needed (explain why)

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have added tests that prove my fix/feature works
  • New and existing tests pass locally
  • I have updated documentation if needed

…elative memory.db

Fixes mem0ai#5004

The default historyDbPath in DEFAULT_MEMORY_CONFIG was set to the bare
relative string "memory.db", which caused better-sqlite3 to resolve it
against the current working directory. Running any CLI command (e.g.
via openclaw-mem0) from different directories created a fresh memory.db
file in each CWD.

Fix mirrors the existing getDefaultVectorStoreDbPath() pattern:
- Add getDefaultHistoryDbPath() → ~/.mem0/history.db in utils/sqlite.ts
- Use it in config/defaults.ts instead of the bare relative literal
- Update 2 test assertions that checked for the old "memory.db" default

Explicitly passed historyDbPath values (relative or absolute) are
unaffected — the config cascade precedence is unchanged.
Copy link
Copy Markdown

@PHclaw PHclaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice fix! Using ~/.mem0/history.db as default is much better than a relative path.

One suggestion: consider using os.path.expanduser() explicitly in case the runtime doesn't expand ~:

default_path = os.path.expanduser('~/.mem0/history.db')

Also, it would be good to ensure the parent directory exists:

os.makedirs(os.path.dirname(default_path), exist_ok=True)

This prevents FileNotFoundError on first run.

@shafdev
Copy link
Copy Markdown
Contributor Author

shafdev commented Apr 29, 2026

Thanks for taking a look! Both points are already handled in the current implementation:

  1. ~ expansion — this is the TypeScript SDK, so we use Node's built-in os.homedir(), which returns the fully-resolved absolute path (e.g. /Users/alice) directly. The ~ literal is never used in the code, so there's nothing to expand.

    // mem0-ts/src/oss/src/utils/sqlite.ts
    export function getDefaultHistoryDbPath(): string {
      return path.join(os.homedir(), ".mem0", "history.db");
    }
  2. Parent directory creationSQLiteManager's constructor already calls ensureSQLiteDirectory(dbPath) before opening the database, which does fs.mkdirSync(path.dirname(dbPath), { recursive: true }). So ~/.mem0/ is auto-created on first run.

    // mem0-ts/src/oss/src/storage/SQLiteManager.ts
    constructor(dbPath: string) {
      ensureSQLiteDirectory(dbPath);
      this.db = new Database(dbPath);
      this.init();
    }

For reference, the Python SDK does the equivalent — os.path.expanduser("~") in mem0/configs/base.py and os.makedirs(mem0_dir, exist_ok=True) in mem0/memory/setup.py. This TS fix mirrors that exact behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mem0 is creating SQLite files in every CWD when using openclaw-mem0 plugin

2 participants