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
# Copyright (c) Huawei Technologies Co., Ltd. 2026. All rights reserved.
"""Subagents must share the parent agent's SysOperation.
The SysOperation is the filesystem boundary the user configured (local full
access / jiuwenbox sandbox / allow-deny paths). When a subagent spec leaves it
unset, ``DeepAgent.create_subagent`` mints a fresh ``OperationMode.LOCAL``
SysOperation with ``restrict_to_sandbox`` derived from
``spec.restrict_to_work_dir or deep_config.restrict_to_work_dir`` — which both
locks the subagent inside the workspace in local mode and lets it escape onto
the host in sandbox mode.
"""
from __future__ import annotations
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
from openjiuwen.harness.schema.config import SubAgentConfig
from openjiuwen.harness.workspace.workspace import Workspace
from jiuwenswarm.server.runtime.agent_adapter.code_agent_rail import AgentTool, CodeAgentRail
from jiuwenswarm.server.runtime.agent_adapter.interface_code import (
JiuwenSwarmCodeAdapter,
)
from jiuwenswarm.server.runtime.agent_adapter.interface_deep import (
JiuWenSwarmDeepAdapter,
_agent_def_to_subagent_config,
)
def _spec_by_name(specs: list, name: str) -> SubAgentConfig | None:
"""Return the spec whose agent card carries ``name``."""
for spec in specs:
if isinstance(spec, SubAgentConfig) and spec.agent_card.name == name:
return spec
return None
def _make_agent_definition(name: str):
"""Build a minimal custom agent definition."""
from jiuwenswarm.server.runtime.agent_config_service import AgentDefinition
return AgentDefinition(
name=name,
description="test",
prompt="You are a test agent.",
source="project",
tools=["*"],
when_to_use="testing",
)
def test_code_subagents_inherit_parent_sys_operation(tmp_path):
adapter = JiuwenSwarmCodeAdapter()
adapter._workspace_dir = str(tmp_path)
adapter._project_dir = str(tmp_path)
adapter._coding_memory_rail = None
sys_operation = MagicMock()
adapter._sys_operation = sys_operation
config = {"subagents": {"code_agent": {"enabled": True}}, "max_iterations": 15}
with patch.object(adapter, "_browser_runtime_enabled", return_value=False):
subagents, _ = adapter._build_configured_subagents(MagicMock(), config, {})
assert subagents is not None
statusline_spec = _spec_by_name(subagents, "statusline-setup")
plan_spec = _spec_by_name(subagents, "plan_agent")
code_spec = _spec_by_name(subagents, "code_agent")
assert statusline_spec is not None
assert plan_spec is not None
assert code_spec is not None
# create_subagent only honours spec.sys_operation when spec.workspace is set too.
assert statusline_spec.sys_operation is sys_operation
assert statusline_spec.workspace == str(tmp_path)
assert plan_spec.sys_operation is sys_operation
assert plan_spec.workspace == str(tmp_path)
assert code_spec.sys_operation is sys_operation
assert code_spec.workspace == str(tmp_path)
def test_deep_research_subagent_inherits_parent_sys_operation(tmp_path):
adapter = JiuWenSwarmDeepAdapter()
adapter._workspace_dir = str(tmp_path)
sys_operation = MagicMock()
adapter._sys_operation = sys_operation
config = {
"subagents": {
"research_agent": {"enabled": True},
"statusline-setup": {"enabled": False},
},
"max_iterations": 15,
}
with patch.object(adapter, "_browser_runtime_enabled", return_value=False):
subagents, _ = adapter._build_configured_subagents(MagicMock(), config, {})
assert subagents is not None
research_spec = _spec_by_name(subagents, "research_agent")
assert research_spec is not None
assert research_spec.sys_operation is sys_operation
assert research_spec.workspace == str(tmp_path)
def test_custom_agent_spec_carries_sys_operation_and_workspace(tmp_path):
sys_operation = MagicMock()
spec = _agent_def_to_subagent_config(
_make_agent_definition("reviewer"),
MagicMock(),
str(tmp_path),
None,
sys_operation,
)
assert spec.sys_operation is sys_operation
assert spec.workspace == str(tmp_path)
def test_agent_tool_subagent_reuses_parent_sys_operation(tmp_path):
sys_operation = MagicMock()
parent_config = MagicMock()
parent_config.model = MagicMock()
parent_config.workspace = Workspace(root_path=str(tmp_path), language="en")
parent_config.language = "en"
parent_config.backend = None
parent_config.max_iterations = 15
parent_config.prompt_mode = None
parent_config.sys_operation = sys_operation
parent_config.enable_read_image_multimodal = False
parent_agent = MagicMock()
parent_agent.deep_config = parent_config
parent_agent.ability_manager.list.return_value = []
agent_def = _make_agent_definition("reviewer")
tool = AgentTool(MagicMock(), parent_agent, [agent_def])
with patch(
"openjiuwen.harness.factory.create_deep_agent",
return_value=MagicMock(),
) as create_deep_agent:
tool._create_sub_agent(agent_def, "session_custom_reviewer_1")
assert create_deep_agent.call_args.kwargs["sys_operation"] is sys_operation
assert (
create_deep_agent.call_args.kwargs["enable_read_image_multimodal"]
is False
)
def test_code_agent_rail_hot_switch_rebinds_custom_agent_registry():
"""Switching workspace must drop the old custom-agent tool and load the new dir."""
rail = CodeAgentRail(workspace_dir="/old-workspace")
agent = MagicMock()
old_def = _make_agent_definition("old_agent")
new_def = _make_agent_definition("new_agent")
with patch.object(
rail, "_load_custom_agents", side_effect=[[old_def], [new_def]]
) as load_custom:
rail.init(agent)
assert rail._agent_tool is not None
assert "old_agent" in rail._agent_tool._custom_agents
rail.set_workspace_dir("/new-workspace")
assert rail._workspace_dir == "/new-workspace"
assert load_custom.call_count == 2
assert rail._agent_tool is not None
assert "new_agent" in rail._agent_tool._custom_agents
assert "old_agent" not in rail._agent_tool._custom_agents
agent.ability_manager.remove_ability.assert_called()
assert agent.ability_manager.add_ability.call_count == 2
def test_code_agent_rail_set_workspace_dir_is_noop_for_same_path():
rail = CodeAgentRail(workspace_dir="/same")
agent = MagicMock()
with patch.object(rail, "_load_custom_agents", return_value=[]):
rail.init(agent)
rail.set_workspace_dir("/same")
rail.set_workspace_dir(" /same ")
assert rail._workspace_dir == "/same"
agent.ability_manager.add_ability.assert_not_called()
def test_code_agent_rail_set_workspace_dir_rolls_back_on_workspace_error():
rail = CodeAgentRail(workspace_dir="/old-workspace")
old_workspace = object()
agent = MagicMock()
agent.deep_config.workspace = old_workspace
with patch.object(rail, "_load_custom_agents", return_value=[]), patch(
"jiuwenswarm.server.runtime.agent_adapter.code_agent_rail.Workspace",
side_effect=ValueError("bad root"),
):
rail.init(agent)
rail.set_workspace_dir("/new-workspace")
assert rail._workspace_dir == "/old-workspace"
assert agent.deep_config.workspace is old_workspace
def _write_project_agent(workspace: Path, name: str) -> Path:
agents_dir = workspace / ".jiuwenswarm" / "agents"
agents_dir.mkdir(parents=True, exist_ok=True)
path = agents_dir / f"{name}.md"
path.write_text(
(
f"---\nname: {name}\ndescription: live {name}\n"
"when_to_use: testing\n---\n\n"
f"You are {name}.\n"
),
encoding="utf-8",
)
return path
def test_code_agent_rail_live_hot_switch_loads_real_files_and_creates_child(
tmp_path, monkeypatch
):
"""Live G: real agent markdown files, no parent rebuild, then create child.
After ``set_workspace_dir``, the new custom agent must be visible and the
created child must inherit the new workspace root plus the same
SysOperation instance.
"""
old_root = tmp_path / "old"
new_root = tmp_path / "new"
_write_project_agent(old_root, "old_reviewer")
_write_project_agent(new_root, "new_reviewer")
monkeypatch.setattr(
"jiuwenswarm.server.runtime.agent_config_service.get_user_workspace_dir",
lambda: tmp_path / "empty-user-home",
)
monkeypatch.setattr(
"jiuwenswarm.common.config.get_config",
lambda: {
"react": {
"subagents": {
"old_reviewer": {"enabled": True},
"new_reviewer": {"enabled": True},
}
}
},
)
sys_operation = MagicMock()
parent_config = MagicMock()
parent_config.model = MagicMock()
parent_config.workspace = Workspace(root_path=str(old_root), language="en")
parent_config.language = "en"
parent_config.backend = None
parent_config.max_iterations = 15
parent_config.prompt_mode = None
parent_config.sys_operation = sys_operation
parent_config.enable_read_image_multimodal = False
parent_agent = MagicMock()
parent_agent.deep_config = parent_config
parent_agent.ability_manager.list.return_value = []
parent_agent.card = SimpleNamespace(id="parent-live-g")
rail = CodeAgentRail(workspace_dir=str(old_root))
rail.init(parent_agent)
assert rail._agent_tool is not None
assert "old_reviewer" in rail._agent_tool._custom_agents
assert "new_reviewer" not in rail._agent_tool._custom_agents
rail.set_workspace_dir(str(new_root))
assert rail._workspace_dir == str(new_root)
assert rail._agent_tool is not None
assert "new_reviewer" in rail._agent_tool._custom_agents
assert "old_reviewer" not in rail._agent_tool._custom_agents
assert parent_config.workspace.root_path == str(new_root)
new_def = rail._agent_tool._custom_agents["new_reviewer"]
with patch(
"openjiuwen.harness.factory.create_deep_agent",
return_value=MagicMock(),
) as create_deep_agent:
rail._agent_tool._create_sub_agent(
new_def,
"session_custom_new_reviewer_1",
)
child_workspace = create_deep_agent.call_args.kwargs["workspace"]
assert create_deep_agent.call_args.kwargs["sys_operation"] is sys_operation
assert str(new_root) in str(child_workspace.root_path)
assert str(old_root) not in str(child_workspace.root_path)