feat(openclaw-plugin): support multi-agent memory isolation via hook context agentId#637
Merged
MaojiaSheng merged 1 commit intovolcengine:mainfrom Mar 15, 2026
Conversation
…context agentId When OpenClaw gateway serves multiple agents, each agent's before_agent_start and agent_end hooks now carry the agent's ID in the second parameter (PluginHookAgentContext). The plugin dynamically switches the client's agentId before each recall/capture operation, ensuring memories are routed to the correct agent_space (md5(user_id + agent_id)[:12]). Changes: - client.ts: Add setAgentId()/getAgentId() to allow dynamic agent switching. Clears cached runtimeIdentity and resolvedSpaceByScope when switching to ensure correct space derivation. - index.ts: Extract agentId from hook ctx (2nd param) in both before_agent_start and agent_end handlers. This is backward compatible: if ctx.agentId is absent (single-agent setup), the plugin falls back to the static config agentId as before.
|
Mac seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
MaojiaSheng
approved these changes
Mar 15, 2026
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.
Summary
When OpenClaw gateway serves multiple agents, each agent's
before_agent_startandagent_endhooks carry the agent's ID in the second parameter (PluginHookAgentContext). This PR makes the memory plugin dynamically switch the client'sagentIdbefore each recall/capture operation, ensuring memories are routed to the correctagent_space(md5(user_id + agent_id)[:12]).Problem
In a multi-agent deployment (e.g., 9 agents sharing one OpenClaw gateway), all agents stored and retrieved memories from the same default agent space (
md5('default' + 'default')[:12]), causing memory cross-contamination between agents.Changes
client.tssetAgentId()/getAgentId()methods toOpenVikingClientsetAgentId()clears cachedruntimeIdentityandresolvedSpaceByScopeto force re-derivation of the agent_space for the new agentindex.tsbefore_agent_starthandler: accept 2ndctxparameter, extractctx.agentId, callclient.setAgentId()before memory recallagent_endhandler: same pattern before memory captureBackward Compatibility
Fully backward compatible. If
ctx?.agentIdis absent (single-agent setup or older OpenClaw versions), the plugin falls back to the staticconfig.agentIdas before.Tested
Verified on OpenClaw v2026.3.13 multi-agent gateway with 9 agents. Gateway logs confirm different agentIds for different agents: