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(team): default swarm worktree members to project workspace

openJiuwen-bot397b6c89d900
diff --git a/jiuwenswarm/agents/harness/team/team_runtime_inheritance.py b/jiuwenswarm/agents/harness/team/team_runtime_inheritance.py
index 219ae549e7..3ecdc30f5f 100644
--- a/jiuwenswarm/agents/harness/team/team_runtime_inheritance.py
+++ b/jiuwenswarm/agents/harness/team/team_runtime_inheritance.py
@@ -127,8 +127,6 @@ TOOL_WHITELIST = frozenset({
"web_fetch_webpage",
"web_paid_search",
"skill_toolkit",
- "enter_worktree",
- "exit_worktree",
"acp_chat",
})
diff --git a/jiuwenswarm/agents/swarm/assembly.py b/jiuwenswarm/agents/swarm/assembly.py
index 6edff0fd56..20e1e779b7 100644
--- a/jiuwenswarm/agents/swarm/assembly.py
+++ b/jiuwenswarm/agents/swarm/assembly.py
@@ -25,6 +25,7 @@ from typing import Any
from openjiuwen.agent_evolving.trajectory import InMemoryTrajectoryRegistry
from openjiuwen.agent_teams.paths import team_home
+from openjiuwen.agent_teams.schema.deep_agent_spec import WorkspaceSpec
from jiuwenswarm.agents.swarm.config_specs import build_member_deep_agent_spec
from jiuwenswarm.agents.swarm.context import SwarmBuildContext
@@ -39,6 +40,29 @@ logger = logging.getLogger(__name__)
_MEMBER_ROLES: tuple[str, ...] = ("leader", "teammate")
+def _with_project_workspace(member_spec: Any, project_dir: str | None) -> Any:
+ """Default a member workspace to the request project directory."""
+ project_root = str(project_dir or "").strip()
+ if not project_root:
+ return member_spec
+
+ workspace = getattr(member_spec, "workspace", None)
+ if workspace is not None and str(getattr(workspace, "root_path", "") or "").strip() not in {"", "./"}:
+ return member_spec
+
+ if workspace is None:
+ workspace = WorkspaceSpec(root_path=project_root)
+ else:
+ workspace = workspace.model_copy(update={"root_path": project_root})
+ return member_spec.model_copy(update={"workspace": workspace})
+
+
+def _worktree_enabled(spec: Any) -> bool:
+ """Return whether the team spec requested managed worktree isolation."""
+ worktree = getattr(spec, "worktree", None)
+ return bool(worktree is not None and getattr(worktree, "enabled", False))
+
+
def enrich_team_spec_for_swarm(
spec: Any,
*,
@@ -98,7 +122,7 @@ def enrich_team_spec_for_swarm(
for role in _MEMBER_ROLES:
if role in spec.agents:
- spec.agents[role] = build_member_deep_agent_spec(
+ member_spec = build_member_deep_agent_spec(
config,
mode,
role,
@@ -106,6 +130,9 @@ def enrich_team_spec_for_swarm(
enable_permissions=spec.enable_permissions,
mcp_configs=mcp_configs,
)
+ if _worktree_enabled(spec):
+ member_spec = _with_project_workspace(member_spec, project_dir)
+ spec.agents[role] = member_spec
spec.build_context = base
# Carry a serializable seed alongside the live context so members rebuilt
diff --git a/jiuwenswarm/agents/swarm/config_specs.py b/jiuwenswarm/agents/swarm/config_specs.py
index 4460a59e21..61e4b2f19d 100644
--- a/jiuwenswarm/agents/swarm/config_specs.py
+++ b/jiuwenswarm/agents/swarm/config_specs.py
@@ -110,7 +110,6 @@ _CODE_RAIL_NAMES: tuple[str, ...] = (
registry.CODE_AGENT_RAIL,
registry.USER_HOOKS,
registry.CODE_SKILL_USE,
- registry.CODE_WORKTREE,
registry.SKILL_RETRIEVAL_PROMPT,
)
diff --git a/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py b/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py
index 941968ee40..01cd3f34ac 100644
--- a/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py
+++ b/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py
@@ -4187,7 +4187,7 @@ class JiuWenSwarmDeepAdapter:
runtime_cwd = str(self._project_dir or "").strip()
if not runtime_cwd or not os.path.isdir(runtime_cwd):
runtime_cwd = workspace_root
- init_cwd(runtime_cwd, workspace=workspace_root)
+ init_cwd(runtime_cwd, project_root=workspace_root, workspace=workspace_root)
@dataclass
class _RuntimeConfig:
diff --git a/tests/agents/swarm/test_swarm_assembly.py b/tests/agents/swarm/test_swarm_assembly.py
index 032cdc7766..78a8dbab21 100644
--- a/tests/agents/swarm/test_swarm_assembly.py
+++ b/tests/agents/swarm/test_swarm_assembly.py
@@ -48,6 +48,7 @@ from openjiuwen.core.single_agent.rail.base import (
AgentRail,
ToolCallInputs,
)
+from openjiuwen.harness.tools.worktree import WorktreeConfig
from openjiuwen.harness.prompts.builder import SystemPromptBuilder
from openjiuwen.harness.rails import SkillUseRail
@@ -673,6 +674,57 @@ def test_enrich_team_spec_for_swarm_rewrites_spec_in_place() -> None:
assert not hasattr(spec, "agent_customizer")
+def test_enrich_team_spec_defaults_member_workspace_to_project_dir() -> None:
+ """Core receives project-rooted member workspaces from swarm enrichment."""
+ spec = _make_team_spec()
+ spec.worktree = WorktreeConfig(enabled=True)
+
+ enrich_team_spec_for_swarm(
+ spec,
+ session_id="s",
+ mode="code.team",
+ project_dir="/tmp/project",
+ channel_id="web",
+ )
+
+ assert spec.agents["leader"].workspace.root_path == "/tmp/project"
+ assert spec.agents["teammate"].workspace.root_path == "/tmp/project"
+
+
+def test_enrich_team_spec_leaves_workspace_when_worktree_disabled() -> None:
+ """Non-worktree teams keep their existing member workspace semantics."""
+ spec = _make_team_spec()
+
+ enrich_team_spec_for_swarm(
+ spec,
+ session_id="s",
+ mode="code.team",
+ project_dir="/tmp/project",
+ channel_id="web",
+ )
+
+ assert spec.agents["leader"].workspace is None
+ assert spec.agents["teammate"].workspace is None
+
+
+def test_enrich_team_spec_preserves_explicit_member_workspace() -> None:
+ """A configured member workspace is not overwritten by project_dir."""
+ spec = _make_team_spec()
+ spec.worktree = WorktreeConfig(enabled=True)
+ spec.agents["leader"].workspace = WorkspaceSpec(root_path="/tmp/custom")
+
+ enrich_team_spec_for_swarm(
+ spec,
+ session_id="s",
+ mode="code.team",
+ project_dir="/tmp/project",
+ channel_id="web",
+ )
+
+ assert spec.agents["leader"].workspace.root_path == "/tmp/custom"
+ assert spec.agents["teammate"].workspace.root_path == "/tmp/project"
+
+
def test_enrich_team_spec_appends_after_existing_rails(monkeypatch) -> None:
"""Provider rails are appended after a member's pre-existing rails."""
monkeypatch.setattr(
@@ -1192,7 +1244,6 @@ _EXPECTED_CODE_RAIL_NAMES: frozenset[str] = frozenset(
registry.CODE_AGENT_RAIL,
registry.USER_HOOKS,
registry.CODE_SKILL_USE,
- registry.CODE_WORKTREE,
registry.SKILL_RETRIEVAL_PROMPT,
registry.CODE_CONFIRM_INTERRUPT,
registry.MEMBER_SKILL_TOOLKIT,
@@ -1666,9 +1717,9 @@ def test_code_member_builds_declaratively_without_post_processing(
"CodeTaskPlanningRail",
"CodeAgentModeRail",
"StructuredAskUserRail",
- "WorktreeRail",
):
assert expected in rail_types, (expected, sorted(rail_types))
+ assert "WorktreeRail" not in rail_types
# The code system prompt is set declaratively on the spec.
assert agent.deep_config.system_prompt
# CodingMemoryRail is published for the code_agent sub-agent to reuse.
@@ -1861,7 +1912,7 @@ def test_rebuilt_member_spec_keeps_provider_declarations() -> None:
rail_types = {rail.type for rail in teammate.rails}
assert registry.CODE_TASK_PLANNING in rail_types
assert registry.CODE_AGENT_MODE in rail_types
- assert registry.CODE_WORKTREE in rail_types
+ assert registry.CODE_WORKTREE not in rail_types
# The code runtime prompt replaces the chat-team common runtime prompt.
assert registry.CODE_RUNTIME_PROMPT in rail_types
assert registry.RUNTIME_PROMPT not in rail_types
diff --git a/tests/unit_tests/agentserver/test_team_runtime_inheritance.py b/tests/unit_tests/agentserver/test_team_runtime_inheritance.py
index ca6d146d87..977996352e 100644
--- a/tests/unit_tests/agentserver/test_team_runtime_inheritance.py
+++ b/tests/unit_tests/agentserver/test_team_runtime_inheritance.py
@@ -62,6 +62,8 @@ def test_filter_inheritable_ability_cards_includes_extended_swarm_tools():
_make_tool_card("user_todos"),
_make_tool_card("task_tool"),
_make_tool_card("acp_chat"),
+ _make_tool_card("enter_worktree"),
+ _make_tool_card("exit_worktree"),
_make_tool_card("send_file_to_user"),
]
)
@@ -75,6 +77,8 @@ def test_filter_inheritable_ability_cards_includes_extended_swarm_tools():
assert "audio_question_answering" in inherited_names
assert "audio_metadata" in inherited_names
assert "user_todos" in inherited_names
+ assert "enter_worktree" not in inherited_names
+ assert "exit_worktree" not in inherited_names
assert "task_tool" not in inherited_names
assert "send_file_to_user" not in inherited_names