feat(openclaw): per-agent memory isolation via sessionKey#4112
Closed
dominiquedutra wants to merge 1 commit intomem0ai:mainfrom
Closed
feat(openclaw): per-agent memory isolation via sessionKey#4112dominiquedutra wants to merge 1 commit intomem0ai:mainfrom
dominiquedutra wants to merge 1 commit intomem0ai:mainfrom
Conversation
Adds two helper functions inside register(): - extractAgentId(sessionKey): parses agentId from the session key pattern agent:<agentId>:<uuid>; returns undefined for 'main' sessions - effectiveUserId(sessionKey): returns agentId or falls back to cfg.userId Both buildAddOptions and buildSearchOptions now accept an optional sessionKey parameter. The before_agent_start (auto-recall) and agent_end (auto-capture) hooks pass sessionId as sessionKey, so every memory read/write is automatically scoped to the correct agent namespace. Single-agent setups are unaffected: when sessionKey is absent or starts with 'main', effectiveUserId() returns cfg.userId as before. Fixes mem0ai#3998
kedamitch
approved these changes
Mar 7, 2026
11 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In multi-agent OpenClaw setups, all agents share the same
userIdnamespace. Memories stored by one agent are recalled by another, making per-role isolation impossible without manual workarounds.Closes #3998
Solution
Two small helper functions inside
register()that route memory reads/writes to the correctuserIdnamespace automatically — with zero breaking changes for single-agent setups.How it works
OpenClaw session keys follow the pattern
agent:<agentId>:<uuid>for sub-agents, and start withmainfor the primary session. The helpers extract theagentIdfrom the session key and use it as theuserId, falling back tocfg.userIdfor the primary session.sessionKeyis threaded throughbuildAddOptionsandbuildSearchOptions, so every memory read/write in thebefore_agent_startandagent_endhooks is automatically scoped to the correct agent.Backward compatibility
sessionKeyisundefinedor starts withmain, soeffectiveUserId()returnscfg.userId— behavior is unchanged.Changes
extractAgentId(sessionKey)helper insideregister()effectiveUserId(sessionKey)helper insideregister()buildAddOptionsaccepts optionalsessionKeyparameterbuildSearchOptionsaccepts optionalsessionKeyparameterbefore_agent_start(auto-recall) andagent_end(auto-capture) hooks threadsessionIdassessionKeyHow to Test
openclaw.config.yaml, e.g.anaandclawd, both using the same mem0 plugin with the sameuserId.anasession, call thememory_storetool to store a fact: "Ana prefers concise answers".clawdsession, callmemory_searchfor "preferences" — the result should be empty (no cross-contamination).ana, callmemory_searchfor "preferences" — the stored fact should be returned.cfg.userIdas before.Production status
This patch has been running in production on a 3-agent setup for several days with a self-hosted Qdrant vector store. No regressions observed.