Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(core): add separator to agent_space_name to prevent hash collisions (#609)

Matt Van Horn76932d3e9570
diff --git a/bot/vikingbot/openviking_mount/ov_server.py b/bot/vikingbot/openviking_mount/ov_server.py
index 935ce1623..d3f6383cc 100644
--- a/bot/vikingbot/openviking_mount/ov_server.py
+++ b/bot/vikingbot/openviking_mount/ov_server.py
@@ -91,7 +91,7 @@ class VikingClient:
}
def get_agent_space_name(self, user_id: str) -> str:
- return hashlib.md5((user_id + self.agent_id).encode()).hexdigest()[:12]
+ return hashlib.md5(f"{user_id}:{self.agent_id}".encode()).hexdigest()[:12]
async def find(self, query: str, target_uri: Optional[str] = None):
"""搜索资源"""
diff --git a/examples/openclaw-memory-plugin/client.ts b/examples/openclaw-memory-plugin/client.ts
index 9dd28bd58..564899991 100644
--- a/examples/openclaw-memory-plugin/client.ts
+++ b/examples/openclaw-memory-plugin/client.ts
@@ -131,10 +131,10 @@ export class OpenVikingClient {
const identity = await this.getRuntimeIdentity();
const fallbackSpace =
- scope === "user" ? identity.userId : md5Short(`${identity.userId}${identity.agentId}`);
+ scope === "user" ? identity.userId : md5Short(`${identity.userId}:${identity.agentId}`);
const reservedDirs = scope === "user" ? USER_STRUCTURE_DIRS : AGENT_STRUCTURE_DIRS;
const preferredSpace =
- scope === "user" ? identity.userId : md5Short(`${identity.userId}${identity.agentId}`);
+ scope === "user" ? identity.userId : md5Short(`${identity.userId}:${identity.agentId}`);
try {
const entries = await this.ls(`viking://${scope}`);
diff --git a/openviking_cli/session/user_id.py b/openviking_cli/session/user_id.py
index cd959e16d..490f4dff2 100644
--- a/openviking_cli/session/user_id.py
+++ b/openviking_cli/session/user_id.py
@@ -51,7 +51,7 @@ class UserIdentifier(object):
def agent_space_name(self) -> str:
"""Agent-level space name (user + agent)."""
- return hashlib.md5((self._user_id + self._agent_id).encode()).hexdigest()[:12]
+ return hashlib.md5(f"{self._user_id}:{self._agent_id}".encode()).hexdigest()[:12]
def memory_space_uri(self) -> str:
return f"viking://agent/{self.agent_space_name()}/memories"
diff --git a/tests/cli/test_user_identifier.py b/tests/cli/test_user_identifier.py
new file mode 100644
index 000000000..f2523a6ab
--- /dev/null
+++ b/tests/cli/test_user_identifier.py
@@ -0,0 +1,32 @@
+"""Tests for UserIdentifier, specifically agent_space_name collision safety."""
+
+from openviking_cli.session.user_id import UserIdentifier
+
+
+class TestAgentSpaceNameCollision:
+ """Verify that agent_space_name uses a separator to prevent hash collisions."""
+
+ def test_different_pairs_produce_different_hashes(self):
+ """Pairs like (alice, bot) vs (aliceb, ot) must not collide."""
+ u1 = UserIdentifier("acct", "alice", "bot")
+ u2 = UserIdentifier("acct", "aliceb", "ot")
+ assert u1.agent_space_name() != u2.agent_space_name()
+
+ def test_same_pair_produces_same_hash(self):
+ """Same (user_id, agent_id) must always produce the same hash."""
+ u1 = UserIdentifier("acct", "alice", "bot")
+ u2 = UserIdentifier("acct", "alice", "bot")
+ assert u1.agent_space_name() == u2.agent_space_name()
+
+ def test_swapped_ids_produce_different_hashes(self):
+ """(user_id=a, agent_id=b) vs (user_id=b, agent_id=a) must differ."""
+ u1 = UserIdentifier("acct", "alpha", "beta")
+ u2 = UserIdentifier("acct", "beta", "alpha")
+ assert u1.agent_space_name() != u2.agent_space_name()
+
+ def test_hash_length(self):
+ """agent_space_name must return a 12-character hex string."""
+ u = UserIdentifier("acct", "user1", "agent1")
+ name = u.agent_space_name()
+ assert len(name) == 12
+ assert all(c in "0123456789abcdef" for c in name)