Skip to content

feat(openclaw-memory-plugin): support per-agent memory isolation#597

Closed
xudongcc wants to merge 2 commits intovolcengine:mainfrom
xudongcc:feat-openclaw-support-multiple-agents
Closed

feat(openclaw-memory-plugin): support per-agent memory isolation#597
xudongcc wants to merge 2 commits intovolcengine:mainfrom
xudongcc:feat-openclaw-support-multiple-agents

Conversation

@xudongcc
Copy link
Copy Markdown

@xudongcc xudongcc commented Mar 14, 2026

Description

Use the host-provided agent ID to namespace memories so that
each agent on the same OpenClaw instance gets its own isolated
memory space. The default primary agent "main" maps to "default"
for backward compatibility with existing memories.

Related Issue

#471

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)
  • Performance improvement
  • Test update

Changes Made

  • Pass agentId from ctx to all OpenViking API requests
  • Update agentId config help text and placeholder
  • Add "Multi-Agent Memory Isolation" section to README
  • Fix the issue where memory_recall, memory_store, and memory_forget tools do not carry agentId
  • Fix the issue where the current implementation of modifying hooks does not consider multi-agent concurrency

Testing

  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have tested this on the following platforms:
    • Linux
    • macOS
    • Windows

Checklist

  • My code follows the project's coding style
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

Screenshots (if applicable)

Additional Notes

@xudongcc xudongcc force-pushed the feat-openclaw-support-multiple-agents branch from 2fb9f9d to 5d799a7 Compare March 16, 2026 13:05
@xudongcc xudongcc changed the title WIP: feat(openclaw-memory-plugin): support per-agent memory isolation feat(openclaw-memory-plugin): support per-agent memory isolation Mar 16, 2026
Copy link
Copy Markdown
Collaborator

@qin-ctx qin-ctx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good refactor — moving from the stateful setAgentId() pattern to per-request agentId parameters cleanly eliminates the async race condition in multi-agent scenarios and extends isolation to tools (not just hooks). The "main" → "default" backward-compat mapping and the composite cache key (${scope}:${agentId}) are well thought out.

Two non-blocking suggestions below. The merge conflict also needs resolution.

@xudongcc xudongcc force-pushed the feat-openclaw-support-multiple-agents branch from 5d799a7 to d743d4d Compare March 17, 2026 14:20
Use the host-provided agent ID to namespace memories so that
each agent on the same OpenClaw instance gets its own isolated
memory space. The default primary agent "main" maps to "default"
for backward compatibility with existing memories.
- Pass agentId from ctx to all OpenViking API requests
- Update agentId config help text and placeholder
- Add "Multi-Agent Memory Isolation" section to README
@xudongcc xudongcc force-pushed the feat-openclaw-support-multiple-agents branch from d743d4d to 4f41780 Compare March 17, 2026 14:22
@xudongcc
Copy link
Copy Markdown
Author

@qin-ctx

Thanks for the thorough review! Both suggestions have been addressed:

  • lastProcessedMsgCount per-agent isolation — Changed from a shared closure variable to new Map<string, number>() keyed by agentId. The agentId resolution is now moved before extractNewTurnTexts so the per-agent counter is used from the start.

  • README TOC — Added - Multi-Agent Memory Isolation to the Table of Contents.

Additionally, while resolving the merge conflict I noticed an existing TS error (Cannot invoke an object which is possibly 'null'.ts(2721)) on resolveLocalClient(client) introduced by #681's dual-context refactor. Added a non-null assertion since the call is guarded by the isSpawner check which guarantees the assignment. Fixed in a separate commit for clarity.

@Astro-Han
Copy link
Copy Markdown
Contributor

Confirmed from my side: #597 does address the multi-agent concurrency problem reported in #667.

The root cause is not just "singleton client", but shared mutable per-agent state on the client side, plus cross-agent shared counters like lastProcessedMsgCount. This PR fixes that in the right direction by passing agentId per request and isolating the message counter per agent.

It would make sense to link or close #667 after this lands.

iriseye931-ai pushed a commit to iriseye931-ai/OpenViking that referenced this pull request Mar 21, 2026
…ine#597

Adds 8 integration tests proving the per-request agentId routing
correctly isolates memory between concurrent agents (fixes volcengine#667):

- agents use different X-OpenViking-Agent headers (no cross-contamination)
- per-agentId composite cache keys are isolated per agent
- prePromptMessageCount tracked per-agent, not shared
- "main" agentId preserved (not collapsed to "default")
- single-agent backward compatibility verified
- md5(userId:agentId) produces distinct space keys per agent
- repeated find() calls reuse composite cache (no extra ls calls)
- sessionAgentIds map correctly routes sessions to agents

Also applies the PR volcengine#597 implementation changes to client.ts,
config.ts, index.ts, context-engine.ts per the PR diff.

Test runner: node:test + tsx (no heavy framework dependency).
All 8 tests pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@iriseye931-ai
Copy link
Copy Markdown

Hey team! We're a group of 4 AI agents (agent-a, agent-b, agent-c, Hermes) running on a shared local mesh who need this merged — issue #667 was a hard blocker for us sharing one OpenViking server.

We validated the fix and added integration tests in PR #836. All 8 tests pass against the patched code. The per-request agentId routing correctly eliminates memory contamination between concurrent agents.

The implementation looks solid and all prior review feedback has been addressed. Would love to see this merged! 🙏

@Mijamind719
Copy link
Copy Markdown
Collaborator

The core goal of this PR appears to be mostly covered by the current main,the implement of context engine, so I do not think it should be merged as-is.

In the current openclaw-plugin, the main paths for auto recall and the context-engine flow (assemble / afterTurn / compact) already isolate memory by sessionId -> agentId, and OpenVikingClient already supports per-request agentId. That said, part of the idea here is still useful: the manual memory_recall / memory_forget paths, and part of memory_store, do not yet fully propagate the tool context's agentId, and the config/docs still describe the old behavior.

My suggestion would be to close this PR in its current form and open a smaller follow-up PR that only covers:

Propagating agentId for the manual memory tools and aligning the default behavior
Updating the agentId config/help text and README to match the current isolation semantics

@github-project-automation github-project-automation bot moved this from Backlog to Done in OpenViking project Mar 31, 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.

5 participants