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: add developer guide md
huanggawa457035fe8c14
diff --git a/docs/en/developer_guide.md b/docs/en/developer_guide.mdnew file mode 100644index 0000000000..83809d311a--- /dev/null+++ b/docs/en/developer_guide.md@@ -0,0 +1,221 @@+# Developer Guide++This document is intended for developers of the JiuwenClaw project, covering how to set up a development environment from source and run tests.++## Prerequisites++| Dependency | Version Requirement | Description |+|------------|---------------------|-------------|+| Operating System | Windows 10/11, macOS 10.15+, Linux | Mainstream OS supported |+| Python | ≥3.11, <3.14 | Python 3.11 recommended |+| Git | Latest | For cloning the repository |+| Node.js | ≥18.x | For frontend interface |+| Bun | Latest | For building TUI frontend packages |++## 1. Clone the Repository++```bash+git clone <repository-url> jiuwenclaw+cd jiuwenclaw+```++## 2. Set Up Development Environment with `uv`++### Install `uv`++If you haven't installed `uv` yet, install it first:++```bash+# macOS / Linux+curl -LsSf https://astral.sh/uv/install.sh | sh++# Windows+powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"+```++### Create a Virtual Environment++Use `uv` to create a new virtual environment (supports Python 3.11, 3.12, or 3.13):++```bash+uv venv --python=3.11+```++### Activate the Environment++```bash+# macOS / Linux+source .venv/bin/activate++# Windows+.venv\Scripts\activate+```++### Run uv Sync++Run the following command in the project root directory `jiuwenclaw/`:++```bash+uv sync+```++This command installs all dependencies based on `pyproject.toml` and `uv.lock`, including development dependencies (such as `pytest`, `pytest-cov`, `pytest-asyncio`, etc.).++## 3. Install Bun (For Building TUI Frontend Packages)++The TUI frontend packages require [Bun](https://bun.sh/) for compilation. Installation methods:++```bash+# macOS, Linux, and WSL+curl -fsSL https://bun.sh/install | bash++# Windows (via PowerShell)+powershell -c "irm bun.sh/install.ps1 | iex"++# Or install via npm+npm install -g bun+```++After installation, verify:++```bash+bun --version+```++## 4. Development++Once the environment is set up, you can start developing the source code and test cases.++### Project Structure Overview++```+jiuwenclaw/+├── jiuwenclaw/ # Project source code+├── tests/+│ ├── unit_tests/ # Unit tests+│ ├── system_tests/ # System tests+│ └── ...+├── docs/ # Documentation+├── pyproject.toml # Project configuration and dependency declarations+├── uv.lock # Dependency lock file+└── pytest.ini # pytest configuration+```++### Adding Dependencies++- **Runtime dependencies**: `uv add <package>`+- **Development dependencies** (testing, linting, etc.): `uv add --dev <package>`++For example:++```bash+uv add --dev pytest-cov pytest-asyncio+```++### Self-Verification Development Workflow++After modifying code, you need to build and verify according to the type of changes made:++#### Backend Code Changes++After modifying backend Python code, run the following commands to reinitialize and start the service:++```bash+uv run jiuwenclaw-init+uv run jiuwenclaw-start+```++#### Frontend Code Changes++After modifying frontend code, rebuild the frontend assets:++```bash+npm run build+```++#### TUI Changes++After modifying TUI (Terminal User Interface) related code, run the development mode for debugging:++```bash+npm run dev+```++## 5. Running Tests++> **Important**: Always use `uv run pytest` instead of running `pytest` directly to ensure that dependencies from the `uv`-managed virtual environment are used.++### Run All Unit Tests++```bash+uv run pytest tests/unit_tests/+```++### Run All System Tests++```bash+uv run pytest tests/system_tests/+```++### Run a Specific Test File++```bash+uv run pytest tests/unit_tests/agentserver/test_team_config_loader.py+```++### Troubleshooting++**Issue: `ModuleNotFoundError: No module named 'xxx'`**++Cause: The system `pytest` was run directly without using the `uv` environment. Solution:++```bash+# Wrong - uses system Python+pytest tests/++# Correct - runs through uv+uv run pytest tests/+```++**Issue: `error: unrecognized arguments: --cov=...`**++Cause: `pytest.ini` is configured with the `--cov` parameter, but `pytest-cov` is not installed in the current environment. Solution:++```bash+uv add --dev pytest-cov pytest-asyncio+```++**Issue: Tests fail unexpectedly due to state leakage between tests**++If a test passes when run individually but fails during a full test run, it is usually because a test file modifies `sys.modules` or global state at the module level, affecting the collection or execution of subsequent tests. You can use binary search to locate the issue:++```bash+# Runs successfully on its own+uv run pytest tests/unit_tests/xxx/test_foo.py++```++**Issue: How to install Node.js**++Frontend development requires Node.js. It is recommended to install via [nvm](https://github.com/nvm-sh/nvm) (Node Version Manager):++```bash+# Install nvm (macOS / Linux)+curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash++# Reload shell configuration+source ~/.bashrc++# Install and use Node.js 18+nvm install 18+nvm use 18+```++Windows users can use [nvm-windows](https://github.com/coreybutler/nvm-windows) or download the installer directly from the [Node.js website](https://nodejs.org/).++After installation, verify the version:++```bash+node --version+# Expected output: v18.x.x or higher+```diff --git a/docs/zh/developer_guide.md b/docs/zh/developer_guide.mdnew file mode 100644index 0000000000..a36e012fd3--- /dev/null+++ b/docs/zh/developer_guide.md@@ -0,0 +1,221 @@+# 开发者指南++本文档面向 JiuwenClaw 项目的开发者,介绍如何从源码搭建开发环境并运行测试。++## 环境要求++| 依赖项 | 版本要求 | 说明 |+|--------|----------|------|+| 操作系统 | Windows 10/11, macOS 10.15+, Linux | 支持主流操作系统 |+| Python | ≥3.11, <3.14 | 推荐 Python 3.11 |+| Git | 最新版本 | 用于克隆源码 |+| Node.js | ≥18.x | 用于前端界面 |+| Bun | 最新版本 | 用于编译 TUI 前端包 |++## 1. 克隆项目++```bash+git clone <repository-url> jiuwenclaw+cd jiuwenclaw+```++## 2. 使用 `uv` 搭建开发环境++### 安装 `uv` 方式安装++如果尚未安装 `uv`,请先安装:++```bash+# macOS / Linux+curl -LsSf https://astral.sh/uv/install.sh | sh++# Windows+powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"+```++### 创建虚拟环境++使用 `uv` 新建虚拟环境(支持 3.11、3.12、3.13 任一版本):++```bash+uv venv --python=3.11+```++### 激活环境++```bash+# macOS / Linux+source .venv/bin/activate++# Windows+.venv\Scripts\activate+```++### 执行 uv 同步操作++在项目根目录 `jiuwenclaw/` 执行:++```bash+uv sync+```++该命令会根据 `pyproject.toml` 和 `uv.lock` 安装所有依赖,包括开发依赖(如 `pytest`、`pytest-cov`、`pytest-asyncio` 等)。++## 3. 安装 Bun(编译 TUI 前端包)++TUI 前端包需要使用 [Bun](https://bun.sh/) 进行编译。安装方式:++```bash+# macOS、Linux 和 WSL+curl -fsSL https://bun.sh/install | bash++# Windows(通过 PowerShell)+powershell -c "irm bun.sh/install.ps1 | iex"++# 也可通过 npm 安装+npm install -g bun+```++安装完成后,验证:++```bash+bun --version+```++## 4. 开发++完成环境搭建后,即可开始源码及测试用例的开发。++### 项目结构概览++```+jiuwenclaw/+├── jiuwenclaw/ # 项目源码+├── tests/+│ ├── unit_tests/ # 单元测试+│ ├── system_tests/ # 系统测试+│ └── ...+├── docs/ # 文档+├── pyproject.toml # 项目配置与依赖声明+├── uv.lock # 依赖锁定文件+└── pytest.ini # pytest 配置+```++### 添加依赖++- **运行时依赖**:`uv add <package>`+- **开发依赖**(测试、lint 等工具):`uv add --dev <package>`++例如:++```bash+uv add --dev pytest-cov pytest-asyncio+```++### 自验证开发流程++修改代码后,需要根据修改的内容类型进行对应的构建和验证:++#### 后端代码修改++修改后端 Python 代码后,执行以下命令重新初始化并启动服务:++```bash+uv run jiuwenclaw-init+uv run jiuwenclaw-start+```++#### 前端代码修改++修改前端代码后,需要重新构建前端资源:++```bash+npm run build+```++#### TUI 修改++修改 TUI(终端用户界面)相关代码后,运行开发模式进行调试:++```bash+npm run dev+```++## 5. 验证测试用例++> **重要**:务必使用 `uv run pytest` 而非直接运行 `pytest`,以确保使用 `uv` 管理的虚拟环境中的依赖。++### 运行全部单元测试++```bash+uv run pytest tests/unit_tests/+```++### 运行全部系统测试++```bash+uv run pytest tests/system_tests/+```++### 运行指定测试文件++```bash+uv run pytest tests/unit_tests/agentserver/test_team_config_loader.py+```++### 常见问题排查++**问题:`ModuleNotFoundError: No module named 'xxx'`**++原因:直接运行了系统的 `pytest`,未使用 `uv` 环境。解决方案:++```bash+# 错误写法 - 使用了系统 Python+pytest tests/++# 正确写法 - 通过 uv 运行+uv run pytest tests/+```++**问题:`error: unrecognized arguments: --cov=...`**++原因:`pytest.ini` 中配置了 `--cov` 参数,但当前环境未安装 `pytest-cov`。解决方案:++```bash+uv add --dev pytest-cov pytest-asyncio+```++**问题:测试间状态泄漏导致莫名失败**++如果某个测试单独运行通过但在全量运行时失败,通常是因为某个测试文件在模块级别修改了 `sys.modules` 或全局状态,影响了后续测试的收集或执行。排查时可使用二分法定位:++```bash+# 单独运行通过+uv run pytest tests/unit_tests/xxx/test_foo.py++```++**问题:如何安装 Node.js**++前端开发需要 Node.js 环境。推荐通过 [nvm](https://github.com/nvm-sh/nvm)(Node Version Manager)安装:++```bash+# 安装 nvm(macOS / Linux)+curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash++# 重新加载 shell 配置+source ~/.bashrc++# 安装并使用 Node.js 18+nvm install 18+nvm use 18+```++Windows 用户可使用 [nvm-windows](https://github.com/coreybutler/nvm-windows) 或直接从 [Node.js 官网](https://nodejs.org/) 下载安装。++安装完成后,验证版本:++```bash+node --version+# 预期输出:v18.x.x 或更高+```diff --git a/pyproject.toml b/pyproject.tomlindex 0643ab6544..a7c4fcad09 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -99,6 +99,11 @@ jiuwenclaw = ["channels/web/frontend/node_modules/**", "channels/web/frontend/nojiuwenclaw-tui = { path = "./packages/jiuwenclaw-tui" }[dependency-groups]+dev = [+ "pytest>=9.0.3",+ "pytest-asyncio>=1.3.0",+ "pytest-cov>=7.1.0",+]# Testing (keep in sync with optional-dependencies.test)test = ["pytest>=8.3.5",diff --git a/tests/unit_tests/agentserver/memory/test_external_memory_builder.py b/tests/unit_tests/agentserver/memory/test_external_memory_builder.pyindex 5736516369..1408bcdcd4 100644--- a/tests/unit_tests/agentserver/memory/test_external_memory_builder.py+++ b/tests/unit_tests/agentserver/memory/test_external_memory_builder.py@@ -6,12 +6,15 @@ Covers the dispatch table in build_external_memory_rail() by stubbing theagent-core provider / rail classes via sys.modules before import."""+import loggingimport sysfrom pathlib import Pathfrom types import ModuleTypeimport pytest+logger = logging.getLogger(__name__)+# ---------------------------------------------------------------------------# Stub agent-core modules so builder's deferred imports succeed@@ -143,6 +146,40 @@ def _install_jiuwenclaw_stubs():utils_stub.get_agent_workspace_dir = _get_agent_workspace_dir+# ---------------------------------------------------------------------------+# Save real openjiuwen modules before stubbing, restore after import so+# other test modules can still import openjiuwen subpackages.+# ---------------------------------------------------------------------------+_AGENT_CORE_STUB_MODULES = [+ "openjiuwen",+ "openjiuwen.core",+ "openjiuwen.core.memory",+ "openjiuwen.core.memory.external",+ "openjiuwen.harness",+ "openjiuwen.harness.rails",+ "openjiuwen.harness.rails.external_memory_rail",+ "openjiuwen.core.memory.external.openjiuwen_memory_provider",+ "openjiuwen.core.memory.external.mem0_provider",+ "openjiuwen.core.memory.external.openviking_memory_provider",+]++_STUB_ATTR_OVERRIDES = [+ ("openjiuwen.harness.rails", "ExternalMemoryRail"),+ ("openjiuwen.harness.rails.external_memory_rail", "ExternalMemoryRail"),+ ("openjiuwen.core.memory.external.openjiuwen_memory_provider", "OpenJiuwenMemoryProvider"),+ ("openjiuwen.core.memory.external.mem0_provider", "Mem0MemoryProvider"),+ ("openjiuwen.core.memory.external.openviking_memory_provider", "OpenVikingMemoryProvider"),+]++_saved_sys_modules: dict = {+ name: sys.modules[name] for name in _AGENT_CORE_STUB_MODULES if name in sys.modules+}++# Save real jiuwenclaw.common.utils callables before patching+import jiuwenclaw.common.utils as _utils_mod+_saved_utils_get_config_file = _utils_mod.get_config_file+_saved_utils_get_agent_workspace_dir = _utils_mod.get_agent_workspace_dir+_install_jiuwenclaw_stubs()_install_agent_core_stubs()@@ -150,6 +187,56 @@ from jiuwenclaw.agents.harness.common.memory import external_memory_builder as efrom jiuwenclaw.agents.harness.common.memory import external_memory_config as emc # noqa: E402+# Immediately restore real modules/utils so other test modules can collect.+def _restore_agent_core_modules():+ # Restore saved originals, and remove stubs we created for modules that+ # were not in sys.modules before we started.+ for name in _AGENT_CORE_STUB_MODULES:+ if name in _saved_sys_modules:+ sys.modules[name] = _saved_sys_modules[name]+ elif name in sys.modules and not hasattr(sys.modules[name], "__path__"):+ # Only pop bare ModuleType stubs we created (no __path__ means+ # not a real package). Leave real modules alone.+ sys.modules.pop(name, None)+ _utils_mod.get_config_file = _saved_utils_get_config_file+ _utils_mod.get_agent_workspace_dir = _saved_utils_get_agent_workspace_dir++_restore_agent_core_modules()++_SENTINEL = object()++[email protected](autouse=True)+def _isolate_agent_core_stubs():+ """Re-install stubs for this module's tests, restore after."""+ saved_attrs = {}+ for mod_name, attr_name in _STUB_ATTR_OVERRIDES:+ mod = sys.modules.get(mod_name)+ if mod is not None:+ saved_attrs[(mod_name, attr_name)] = getattr(mod, attr_name, _SENTINEL)++ saved_utils_cfg = _utils_mod.get_config_file+ saved_utils_wd = _utils_mod.get_agent_workspace_dir++ _install_agent_core_stubs()+ _install_jiuwenclaw_stubs()+ yield++ for (mod_name, attr_name), val in saved_attrs.items():+ mod = sys.modules.get(mod_name)+ if mod is not None:+ if val is _SENTINEL:+ try:+ delattr(mod, attr_name)+ except AttributeError:+ logger.debug("Attribute %s not found on module %s, skipping deletion", attr_name, mod_name)+ else:+ setattr(mod, attr_name, val)+ _utils_mod.get_config_file = saved_utils_cfg+ _utils_mod.get_agent_workspace_dir = saved_utils_wd+ _restore_agent_core_modules()++@pytest.fixture(autouse=True)def reset_spy_state():_FakeRail.last_args = Nonediff --git a/tests/unit_tests/agentserver/memory/test_external_memory_config.py b/tests/unit_tests/agentserver/memory/test_external_memory_config.pyindex 2697ea1972..ab5c8d542b 100644--- a/tests/unit_tests/agentserver/memory/test_external_memory_config.py+++ b/tests/unit_tests/agentserver/memory/test_external_memory_config.py@@ -57,15 +57,31 @@ def _get_agent_workspace_dir():# Load the real jiuwenclaw.utils (do NOT replace it in sys.modules — that# leaks str-returning stubs into every later test in the session). Patch-# only the two attrs we need; the module-scoped autouse fixture in this-# package's conftest.py restores them after this module's tests finish.+# only the two attrs we need; restore immediately after importing the+# module under test so other test modules can collect.import jiuwenclaw.common.utils as utils_stub # noqa: E402+_saved_get_config_file = utils_stub.get_config_file+_saved_get_agent_workspace_dir = utils_stub.get_agent_workspace_dir+utils_stub.get_config_file = _get_config_fileutils_stub.get_agent_workspace_dir = _get_agent_workspace_dirfrom jiuwenclaw.agents.harness.common.memory import external_memory_config as emc # noqa: E402+utils_stub.get_config_file = _saved_get_config_file+utils_stub.get_agent_workspace_dir = _saved_get_agent_workspace_dir++[email protected](autouse=True)+def _isolate_utils_stubs():+ """Re-apply utils stubs for this module's tests, restore after."""+ utils_stub.get_config_file = _get_config_file+ utils_stub.get_agent_workspace_dir = _get_agent_workspace_dir+ yield+ utils_stub.get_config_file = _saved_get_config_file+ utils_stub.get_agent_workspace_dir = _saved_get_agent_workspace_dir+# ---------------------------------------------------------------------------# get_memory_enginediff --git a/tests/unit_tests/agentserver/test_system_prompt_restructure.py b/tests/unit_tests/agentserver/test_system_prompt_restructure.pyindex 5021d8f672..85ddd6edc0 100644--- a/tests/unit_tests/agentserver/test_system_prompt_restructure.py+++ b/tests/unit_tests/agentserver/test_system_prompt_restructure.py@@ -67,7 +67,8 @@ def test_resolve_skill_mode_accepts_all_and_auto_list():assert JiuWenClawDeepAdapter._resolve_skill_mode({"skill_mode": "invalid"}) == "all"-def test_resolve_enable_task_loop_can_be_called_on_class():+def test_resolve_enable_task_loop_can_be_called_on_class(monkeypatch):+ monkeypatch.delenv("SKILL_CREATE", raising=False)assert (JiuWenClawDeepAdapter._resolve_enable_task_loop({"enable_task_loop": False},diff --git a/uv.lock b/uv.lockindex 02de88ea31..d974d6df78 100644--- a/uv.lock+++ b/uv.lock@@ -1568,6 +1568,11 @@ tui = [][package.dev-dependencies]+dev = [+ { name = "pytest" },+ { name = "pytest-asyncio" },+ { name = "pytest-cov" },+]test = [{ name = "coverage" },{ name = "pytest" },@@ -1624,6 +1629,11 @@ requires-dist = [provides-extras = ["desktop", "dev", "tui", "distribute", "shell-ast", "test"][package.metadata.requires-dev]+dev = [+ { name = "pytest", specifier = ">=9.0.3" },+ { name = "pytest-asyncio", specifier = ">=1.3.0" },+ { name = "pytest-cov", specifier = ">=7.1.0" },+]test = [{ name = "coverage", specifier = ">=7.7.1" },{ name = "pytest", specifier = ">=8.3.5" },