Skip to content

feat(openclaw-plugin): support multi-agent memory isolation via hook context agentId#637

Merged
MaojiaSheng merged 1 commit intovolcengine:mainfrom
yingriyanlong:feat/multi-agent-memory-isolation
Mar 15, 2026
Merged

feat(openclaw-plugin): support multi-agent memory isolation via hook context agentId#637
MaojiaSheng merged 1 commit intovolcengine:mainfrom
yingriyanlong:feat/multi-agent-memory-isolation

Conversation

@yingriyanlong
Copy link
Copy Markdown
Contributor

Summary

When OpenClaw gateway serves multiple agents, each agent's before_agent_start and agent_end hooks carry the agent's ID in the second parameter (PluginHookAgentContext). This PR makes the memory plugin dynamically switch the client's agentId before each recall/capture operation, ensuring memories are routed to the correct agent_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.ts

  • Add setAgentId() / getAgentId() methods to OpenVikingClient
  • setAgentId() clears cached runtimeIdentity and resolvedSpaceByScope to force re-derivation of the agent_space for the new agent

index.ts

  • before_agent_start handler: accept 2nd ctx parameter, extract ctx.agentId, call client.setAgentId() before memory recall
  • agent_end handler: same pattern before memory capture

Backward Compatibility

Fully backward compatible. If ctx?.agentId is absent (single-agent setup or older OpenClaw versions), the plugin falls back to the static config.agentId as before.

Tested

Verified on OpenClaw v2026.3.13 multi-agent gateway with 9 agents. Gateway logs confirm different agentIds for different agents:

memory-openviking: switched to agentId=main for recall
memory-openviking: switched to agentId=main for capture
memory-openviking: switched to agentId=r3-2 for recall
memory-openviking: switched to agentId=r3-2 for capture

…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.
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


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 MaojiaSheng merged commit afc15b5 into volcengine:main Mar 15, 2026
1 check was pending
@github-project-automation github-project-automation bot moved this from Backlog to Done in OpenViking project Mar 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants