Skip to content

fix(memory): scope Mem0 long-term memory by index via agent_id - #1212

Open
eric7h wants to merge 1 commit into
volcengine:mainfrom
eric7h:fix/mem0-ltm-scope-by-index
Open

eric7h wants to merge 1 commit into
volcengine:mainfrom
eric7h:fix/mem0-ltm-scope-by-index

Conversation

@eric7h

@eric7h eric7h commented Nov 6, 2026

Copy link
Copy Markdown

Summary

Mem0LTMBackend logged self.index (from LongTermMemory.app_name) but never passed it to Mem0 add/search. Apps that share one Mem0 project therefore shared memories for the same user_id, unlike VikingDB (collection=index), Redis/OpenSearch (index name includes index), OpenViking (app_name), and TOS ContextBucket (bucket=index).

Also align save_memory(user_id, event_strings, ...) with BaseLongTermMemoryBackend — the previous signature swapped the first two parameters, so a positional call treated the user id as the event list and failed.

Fix

  • Pass agent_id=self.index on Mem0 add and search (Mem0 1.x kwargs API that veadk pins via mem0ai>=1.0,<2).
  • Restore base-class parameter order for save_memory / search_memory.

How to reproduce

# With a fake MemoryClient that stores by (user_id, agent_id):
alpha = Mem0LTMBackend(index="app_alpha", mem0_config=...)
beta = Mem0LTMBackend(index="app_beta", mem0_config=...)
alpha.save_memory(user_id="u1", event_strings=["alpha-only"])
beta.save_memory(user_id="u1", event_strings=["beta-only"])
# Before: both searches returned both memories (agent_id was None).
# After: each search returns only its own index's memories.

New tests in tests/memory/long_term_memory_backends/test_mem0_ltm_backend.py fail on main and pass with this change (3 passed). Related memory suite: 43 passed.

Test plan

  • pytest tests/memory/long_term_memory_backends/test_mem0_ltm_backend.py — 3 passed
  • pytest tests/test_long_term_memory.py tests/memory/ (excluding postgres endpoint) — 43 passed
  • pre-commit (ruff check/format, gitleaks) on touched files

LongTermMemory.index (from app_name) was only logged and never passed to
Mem0, so apps that share one Mem0 project leaked memories across indexes
for the same user_id. Pass agent_id=self.index on add/search to match
how VikingDB/Redis/OpenSearch isolate by index, and align save_memory
parameter order with BaseLongTermMemoryBackend.

This branch has not been deployed

No deployments
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.

1 participant