Skip to content

feat: Per-agent memory isolation for openclaw-mem0 plugin #3998

@kevyn-noocar

Description

@kevyn-noocar

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 key
  • effectiveUserId() — returns agentId for non-main agents, falls back to configured userId
  • Updated buildAddOptions, buildSearchOptions, hooks, and memory_list to use effectiveUserId()

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.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions