-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Description
Problem
In multi-agent OpenClaw setups (one gateway, multiple agents with isolated workspaces), all agents share the same userId for Mem0 operations. This means Agent A's memories are recalled for Agent B and vice versa, breaking domain isolation.
Real-world example: A personal assistant agent and a healthcare clinic assistant agent sharing the same gateway — clinic SOPs and compliance memories bleed into the personal assistant's context.
Proposed Solution
The plugin already receives sessionKey via the hook context (ctx.sessionKey), and session keys follow the pattern agent:<agentId>:.... By extracting the agentId and using it as the effective userId for non-main agents, memories are automatically isolated per agent.
Implementation
We've implemented this and published the patch: https://github.com/iiiiconsulting/openclaw-mem0-per-agent
Key changes (~15 lines):
extractAgentId(sessionKey)— parses agentId from session keyeffectiveUserId()— returns agentId for non-main agents, falls back to configured userId- Updated
buildAddOptions,buildSearchOptions, hooks, andmemory_listto useeffectiveUserId()
Zero breaking changes — default behavior unchanged for single-agent or main-agent setups. Explicit userId parameter overrides still work. All data stays in the same store, logically partitioned by user_id.
Would love to see this in the official plugin. Happy to contribute a PR if there's a repo for @mem0/openclaw-mem0.