Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix(memory):Write "memory.db" to the agent workspace.

openJiuwen-bot41aac3d49798
diff --git a/jiuwenswarm/agents/harness/common/memory_rpc.py b/jiuwenswarm/agents/harness/common/memory_rpc.py
index 0c65c70368..360e661b75 100644
--- a/jiuwenswarm/agents/harness/common/memory_rpc.py
+++ b/jiuwenswarm/agents/harness/common/memory_rpc.py
@@ -22,6 +22,7 @@ from jiuwenswarm.agents.harness.common.rails.project_memory import (
discover_and_load_memory_files,
get_large_memory_files,
)
+from jiuwenswarm.common.coding_memory_paths import resolve_project_coding_memory_dir
from jiuwenswarm.common.config import get_config
logger = logging.getLogger(__name__)
@@ -34,8 +35,10 @@ def _is_forbidden_enabled(config: dict[str, Any] | None) -> bool:
def _get_coding_memory_dir(workspace: str, project_dir: str | None = None) -> str:
- _project_name = os.path.basename(project_dir) if project_dir else "default"
- return os.path.join(workspace, "coding_memory", _project_name)
+ return resolve_project_coding_memory_dir(
+ agent_workspace_dir=workspace,
+ project_dir=project_dir,
+ )
def _get_allowed_dirs(workspace: str, project_dir: str | None = None) -> list[str]:
diff --git a/jiuwenswarm/common/coding_memory_paths.py b/jiuwenswarm/common/coding_memory_paths.py
new file mode 100644
index 0000000000..9c8cc38427
--- /dev/null
+++ b/jiuwenswarm/common/coding_memory_paths.py
@@ -0,0 +1,40 @@
+# Copyright (c) Huawei Technologies Co., Ltd. 2026. All rights reserved.
+
+"""Helpers for resolving project-scoped coding memory paths."""
+
+from __future__ import annotations
+
+import ntpath
+import os
+from os import PathLike
+
+DEFAULT_CODING_MEMORY_PROJECT = "default"
+
+
+def resolve_coding_memory_project_name(project_dir: str | PathLike[str] | None) -> str:
+ """Return the project-scoped directory name used under coding_memory/."""
+ if project_dir is None:
+ return DEFAULT_CODING_MEMORY_PROJECT
+
+ raw_project_dir = str(project_dir).strip()
+ if not raw_project_dir:
+ return DEFAULT_CODING_MEMORY_PROJECT
+
+ project_name = ntpath.basename(raw_project_dir.rstrip("/\\"))
+ project_name = project_name.replace("/", "_").replace("\\", "_").strip()
+ if not project_name or project_name in {".", ".."}:
+ return DEFAULT_CODING_MEMORY_PROJECT
+ return project_name
+
+
+def resolve_project_coding_memory_dir(
+ *,
+ agent_workspace_dir: str | PathLike[str],
+ project_dir: str | PathLike[str] | None,
+) -> str:
+ """Resolve ``<agent_workspace>/coding_memory/<project_name>``."""
+ return os.path.join(
+ os.path.abspath(str(agent_workspace_dir)),
+ "coding_memory",
+ resolve_coding_memory_project_name(project_dir),
+ )
diff --git a/jiuwenswarm/server/runtime/agent_adapter/interface_code.py b/jiuwenswarm/server/runtime/agent_adapter/interface_code.py
index 487c32f826..5aa85b87ae 100644
--- a/jiuwenswarm/server/runtime/agent_adapter/interface_code.py
+++ b/jiuwenswarm/server/runtime/agent_adapter/interface_code.py
@@ -69,7 +69,7 @@ from jiuwenswarm.agents.harness.common.memory.config import get_memory_mode
from jiuwenswarm.agents.harness.common.tools import SkillToolkit
from jiuwenswarm.agents.harness.common.tools.acp_chat import acp_chat
from jiuwenswarm.common.config import get_config
-from jiuwenswarm.common.utils import get_agent_workspace_dir
+from jiuwenswarm.common.coding_memory_paths import resolve_project_coding_memory_dir
from jiuwenswarm.server.runtime.agent_adapter.code_agent_rail import CodeAgentRail
from jiuwenswarm.common.hooks_config import load_hooks_config
from jiuwenswarm.server.hooks.user_hook_rail import UserHookRail
@@ -241,6 +241,63 @@ class _RailBuildInfo:
self.params = self.params or {}
+def _resolve_coding_memory_dir(
+ *,
+ project_dir: str | None,
+ agent_workspace_dir: str,
+) -> str:
+ """Resolve the app-owned CodingMemory directory scoped by project."""
+ return resolve_project_coding_memory_dir(
+ agent_workspace_dir=agent_workspace_dir,
+ project_dir=project_dir,
+ )
+
+
+def _build_coding_memory_directory_node(
+ coding_memory_dir: str,
+ *,
+ description: str,
+) -> dict[str, Any]:
+ return {
+ "name": "coding_memory",
+ "description": description,
+ "path": coding_memory_dir,
+ "children": [
+ {
+ "name": "MEMORY.md",
+ "description": "Coding 记忆索引",
+ "path": "MEMORY.md",
+ "children": [],
+ "is_file": True,
+ "default_content": "",
+ },
+ ],
+ }
+
+
+def _set_workspace_coding_memory_directory(
+ workspace: Any,
+ *,
+ project_dir: str | None,
+ agent_workspace_dir: str,
+ description: str = "Coding Agent memory",
+) -> None:
+ set_directory = getattr(workspace, "set_directory", None)
+ if not callable(set_directory):
+ return
+
+ coding_memory_dir = _resolve_coding_memory_dir(
+ project_dir=project_dir,
+ agent_workspace_dir=agent_workspace_dir,
+ )
+ set_directory(
+ _build_coding_memory_directory_node(
+ coding_memory_dir,
+ description=description,
+ )
+ )
+
+
def create_coding_memory_rail(
*,
project_dir: str | None,
@@ -266,8 +323,10 @@ def create_coding_memory_rail(
"registering tools with memory fallback provider"
)
- project_name = os.path.basename(project_dir) if project_dir else "default"
- coding_memory_dir = os.path.join(agent_workspace_dir, "coding_memory", project_name)
+ coding_memory_dir = _resolve_coding_memory_dir(
+ project_dir=project_dir,
+ agent_workspace_dir=agent_workspace_dir,
+ )
os.makedirs(coding_memory_dir, exist_ok=True)
return CodingMemoryRail(
@@ -632,6 +691,17 @@ class JiuwenClawCodeAdapter(JiuWenClawDeepAdapter):
configured_subagents, _should_add_general = self._build_configured_subagents(model, config, config_base)
configured_subagents = configured_subagents or []
+ workspace = Workspace(
+ root_path=self._workspace_dir or "./",
+ language=self._resolve_runtime_language(),
+ )
+ _set_workspace_coding_memory_directory(
+ workspace,
+ project_dir=self._project_dir,
+ agent_workspace_dir=self._agent_workspace_dir,
+ description="Coding Agent 记忆模块",
+ )
+
self._instance = create_deep_agent(
model=model,
card=agent_card,
@@ -641,10 +711,7 @@ class JiuwenClawCodeAdapter(JiuWenClawDeepAdapter):
rails=rails_list if rails_list else [],
enable_task_loop=config.get("enable_task_loop", True),
max_iterations=config.get("max_iterations", 15),
- workspace=Workspace(
- root_path=self._workspace_dir or "./",
- language=self._resolve_runtime_language(),
- ),
+ workspace=workspace,
sys_operation=sys_operation,
language=self._resolve_runtime_language(),
auto_create_workspace=False
@@ -672,24 +739,6 @@ class JiuwenClawCodeAdapter(JiuWenClawDeepAdapter):
self._project_dir or self._workspace_dir,
)
- _project_name = os.path.basename(self._project_dir) if self._project_dir else "default"
- coding_memory_workspace_path = os.path.join("coding_memory", _project_name)
- self._instance.deep_config.workspace.set_directory({
- "name": "coding_memory",
- "description": "Coding Agent 记忆模块",
- "path": coding_memory_workspace_path,
- "children": [
- {
- "name": "MEMORY.md",
- "description": "Coding 记忆索引",
- "path": "MEMORY.md",
- "children": [],
- "is_file": True,
- "default_content": "",
- },
- ],
- })
-
# code 模式不传: vision_model_config, audio_model_config,
# context_engine_config, completion_timeout
@@ -1502,7 +1551,8 @@ class JiuwenClawCodeAdapter(JiuWenClawDeepAdapter):
or react_config.get("workspace_dir")
or str(get_agent_workspace_dir())
)
- self._agent_workspace_dir = member_workspace_root or str(get_agent_workspace_dir())
+ # Coding memory is application data; keep it out of member/project cwd.
+ self._agent_workspace_dir = str(get_agent_workspace_dir())
self._instance_overrides = {
"agent_name": self._agent_name,
"project_dir": self._project_dir,
@@ -1519,6 +1569,12 @@ class JiuwenClawCodeAdapter(JiuWenClawDeepAdapter):
tool_cards = self.build_code_tool_cards(agent_id)
added_tools = _merge_tool_cards(agent, tool_cards)
+ _set_coding_memory_directory(
+ agent,
+ self._project_dir,
+ self._agent_workspace_dir,
+ )
+
rails = self._build_agent_rails(react_config, config_base, mode="code")
added_rails = sum(1 for rail in rails if _queue_rail_if_missing(agent, rail))
@@ -1530,7 +1586,6 @@ class JiuwenClawCodeAdapter(JiuWenClawDeepAdapter):
if _queue_rail_if_missing(agent, subagent_rail):
added_rails += 1
- _set_coding_memory_directory(agent, self._project_dir)
setattr(agent, "_jiuwenswarm_adapter_mode", "code")
setattr(agent, "_jiuwenswarm_code_project_dir", self._project_dir or self._workspace_dir)
setattr(agent, "_jiuwenswarm_project_dir", self._project_dir or self._workspace_dir)
@@ -1652,30 +1707,19 @@ def _resolve_member_workspace_root(agent: Any) -> str | None:
return None
-def _set_coding_memory_directory(agent: Any, project_dir: str | None) -> None:
+def _set_coding_memory_directory(
+ agent: Any,
+ project_dir: str | None,
+ agent_workspace_dir: str,
+) -> None:
deep_config = getattr(agent, "deep_config", None)
workspace = getattr(deep_config, "workspace", None)
- set_directory = getattr(workspace, "set_directory", None)
- if not callable(set_directory):
- return
-
- project_name = os.path.basename(project_dir) if project_dir else "default"
- coding_memory_workspace_path = os.path.join("coding_memory", project_name)
- set_directory({
- "name": "coding_memory",
- "description": "Coding Agent memory",
- "path": coding_memory_workspace_path,
- "children": [
- {
- "name": "MEMORY.md",
- "description": "Coding memory",
- "path": "MEMORY.md",
- "children": [],
- "is_file": True,
- "default_content": "",
- },
- ],
- })
+ _set_workspace_coding_memory_directory(
+ workspace,
+ project_dir=project_dir,
+ agent_workspace_dir=agent_workspace_dir,
+ description="Coding Agent memory",
+ )
def configure_code_team_member_agent(
diff --git a/tests/unit_tests/agentserver/memory/test_memory_rpc.py b/tests/unit_tests/agentserver/memory/test_memory_rpc.py
new file mode 100644
index 0000000000..b8e5506a5b
--- /dev/null
+++ b/tests/unit_tests/agentserver/memory/test_memory_rpc.py
@@ -0,0 +1,22 @@
+# Copyright (c) Huawei Technologies Co., Ltd. 2026. All rights reserved.
+
+from pathlib import Path
+
+import pytest
+
+from jiuwenswarm.agents.harness.common import memory_rpc
+
+
+async def test_coding_memory_dir_is_project_scoped_under_workspace(tmp_path):
+ workspace = tmp_path / "agent_workspace"
+ project_dir = tmp_path / "project"
+ coding_memory_dir = workspace / "coding_memory" / "project"
+ coding_memory_dir.mkdir(parents=True)
+
+ result = await memory_rpc.handle_memory_open(
+ str(workspace),
+ {"project_dir": str(project_dir)},
+ )
+
+ assert Path(result["coding_memory_dir"]) == coding_memory_dir
diff --git a/tests/unit_tests/agentserver/test_team_manager_skill_state.py b/tests/unit_tests/agentserver/test_team_manager_skill_state.py
index 7bb5671b23..8823d87ea5 100644
--- a/tests/unit_tests/agentserver/test_team_manager_skill_state.py
+++ b/tests/unit_tests/agentserver/test_team_manager_skill_state.py
@@ -336,8 +336,8 @@ def test_team_plan_leader_uses_preferred_language_for_code_profile(monkeypatch,
assert calls[0]["force_english_runtime_prompt"] is False
-def test_configure_code_team_member_uses_relative_coding_memory_path(monkeypatch, tmp_path):
- """code.team members should register workspace directories with relative paths."""
+def test_configure_code_team_member_uses_agent_workspace_coding_memory_path(monkeypatch, tmp_path):
+ """code.team members should keep coding memory out of member cwd."""
from jiuwenswarm.server.runtime.agent_adapter import interface_code
global_workspace = tmp_path / "global_agent_workspace"
@@ -428,5 +428,6 @@ def test_configure_code_team_member_uses_relative_coding_memory_path(monkeypatch
)
coding_memory_path = Path(workspace.directories[0]["path"])
- assert not coding_memory_path.is_absolute()
- assert coding_memory_path.parts == ("coding_memory", parent_project.name)
+ assert coding_memory_path.is_absolute()
+ assert coding_memory_path == global_workspace / "coding_memory" / "project"
+ assert coding_memory_path != member_workspace / "coding_memory"