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(swarm): report and seed the member's real working directory
alan_cheng6478cc2a060c
diff --git a/jiuwenswarm/agents/harness/common/rails/runtime_prompt_rail.py b/jiuwenswarm/agents/harness/common/rails/runtime_prompt_rail.pyindex d9ee8c6c8c..42f6ba80fb 100644--- a/jiuwenswarm/agents/harness/common/rails/runtime_prompt_rail.py+++ b/jiuwenswarm/agents/harness/common/rails/runtime_prompt_rail.py@@ -51,6 +51,7 @@ class RuntimePromptRail(DeepAgentRail):self._trusted_dirs: list[str] | None = Noneself._cwd: str | None = Noneself._project_dir: str | None = None+ self._workspace_dir: str | None = Noneself._model_name: str = ""self._mode: str = ""self._session_id: str | None = None@@ -86,14 +87,33 @@ class RuntimePromptRail(DeepAgentRail):"""per-request 更新可信目录。"""self._trusted_dirs = trusted_dirs- def set_runtime_paths(self, *, cwd: str | None = None, project_dir: str | None = None) -> None:- """Per-request stable project identity and dynamic cwd."""+ def set_runtime_paths(+ self,+ *,+ cwd: str | None = None,+ project_dir: str | None = None,+ workspace_dir: str | None = None,+ ) -> None:+ """Per-request stable project identity, dynamic cwd and own workspace.++ Args:+ cwd: Working directory shell runs in and relative paths resolve against.+ project_dir: Project root, when the request is bound to one.+ workspace_dir: This agent's own workspace (artifacts, memory, skills+ view). Team members each have their own; falls back to the+ process-wide agent workspace when unset.+ """self._cwd = cwd.strip() if isinstance(cwd, str) and cwd.strip() else Noneself._project_dir = (project_dir.strip()if isinstance(project_dir, str) and project_dir.strip()else None)+ self._workspace_dir = (+ workspace_dir.strip()+ if isinstance(workspace_dir, str) and workspace_dir.strip()+ else None+ )def set_model_name(self, model_name: str) -> None:"""per-request 更新模型名称,作为文件读取失败时的兜底。"""@@ -435,7 +455,10 @@ class RuntimePromptRail(DeepAgentRail):if self._channel in ("tui", "web"):# Trusted directories policy for TUI and Web modetrusted_dirs = self._existing_dirs(self._trusted_dirs)- agent_workspace_dir = str(get_agent_workspace_dir())+ # This agent's own workspace. Team members each own one; without+ # it (single-agent runs) the process-wide agent workspace is the+ # same directory anyway.+ agent_workspace_dir = self._existing_dir(self._workspace_dir) or str(get_agent_workspace_dir())config_dir = str(get_user_workspace_dir() / "config")project_dir = self._existing_dir(self._project_dir)runtime_cwd = (diff --git a/jiuwenswarm/agents/swarm/assembly.py b/jiuwenswarm/agents/swarm/assembly.pyindex 20e1e779b7..2e2a5ba54d 100644--- a/jiuwenswarm/agents/swarm/assembly.py+++ b/jiuwenswarm/agents/swarm/assembly.py@@ -25,7 +25,6 @@ from typing import Anyfrom openjiuwen.agent_evolving.trajectory import InMemoryTrajectoryRegistryfrom openjiuwen.agent_teams.paths import team_home-from openjiuwen.agent_teams.schema.deep_agent_spec import WorkspaceSpecfrom jiuwenswarm.agents.swarm.config_specs import build_member_deep_agent_specfrom jiuwenswarm.agents.swarm.context import SwarmBuildContext@@ -40,27 +39,18 @@ 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."""+def _with_project_cwd(member_spec: Any, project_dir: str | None) -> Any:+ """Point a member's cwd / project root at the request project directory.++ Only the working directory moves: the member keeps its own workspace for+ artifacts (memory, skills view, ``.team`` mount). When worktree isolation+ is on, ``AgentConfigurator`` overrides cwd again with the member worktree,+ which is why this is unconditional here.+ """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))+ return member_spec.model_copy(update={"cwd": project_root, "project_root": project_root})def enrich_team_spec_for_swarm(@@ -130,8 +120,7 @@ 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)+ member_spec = _with_project_cwd(member_spec, project_dir)spec.agents[role] = member_specspec.build_context = basediff --git a/jiuwenswarm/agents/swarm/providers/member_rails.py b/jiuwenswarm/agents/swarm/providers/member_rails.pyindex 44a2ae2211..e882c9640b 100644--- a/jiuwenswarm/agents/swarm/providers/member_rails.py+++ b/jiuwenswarm/agents/swarm/providers/member_rails.py@@ -143,6 +143,10 @@ class RuntimePromptInput(ConstructionInput):attr="project_dir",description="Resolved user project directory (seeds the TUI cwd policy).",)+ member_workspace_root: str | None = context_field(+ resolver=_workspace_root,+ description="Current member workspace root (cwd fallback without a project).",+ )@harness_element(@@ -171,12 +175,16 @@ def _build_runtime_prompt_rail(# instead of the process-wide ``default`` state file.rail.set_mode(context.mode)rail.set_session_id(context.session_id)- # Seed cwd/project_dir so the TUI branch injects the "current project- # directory" policy and the model answers with the project dir instead of- # calling `pwd` (which would surface the per-member workspace path).- # Mirrors the code-team rail (code_rails.build_code_runtime_prompt).- if inp.project_dir:- rail.set_runtime_paths(cwd=inp.project_dir, project_dir=inp.project_dir)+ # Report the member's real working directory. cwd and workspace are+ # separate layers (see openjiuwen.core.sys_operation.cwd): with a project+ # the member runs in the project dir, without one it runs in its own+ # workspace. Reporting anything else makes the model resolve relative+ # paths against a directory the tools never use.+ rail.set_runtime_paths(+ cwd=inp.project_dir or inp.member_workspace_root,+ project_dir=inp.project_dir,+ workspace_dir=inp.member_workspace_root,+ )return raildiff --git a/tests/agents/swarm/test_swarm_assembly.py b/tests/agents/swarm/test_swarm_assembly.pyindex 13ec9c5c0d..fbd3ee8f82 100644--- a/tests/agents/swarm/test_swarm_assembly.py+++ b/tests/agents/swarm/test_swarm_assembly.py@@ -817,8 +817,12 @@ 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."""+def test_enrich_team_spec_points_member_cwd_at_project_dir() -> None:+ """Core receives project-rooted member cwd, not a rewritten workspace.++ cwd and workspace are separate layers: members run in the project+ directory while keeping their own workspace for artifacts.+ """spec = _make_team_spec()spec.worktree = WorktreeConfig(enabled=True)@@ -830,8 +834,10 @@ def test_enrich_team_spec_defaults_member_workspace_to_project_dir() -> None:channel_id="web",)- assert spec.agents["leader"].workspace.root_path == "/tmp/project"- assert spec.agents["teammate"].workspace.root_path == "/tmp/project"+ for role in ("leader", "teammate"):+ assert spec.agents[role].cwd == "/tmp/project"+ assert spec.agents[role].project_root == "/tmp/project"+ assert spec.agents[role].workspace is Nonedef test_enrich_team_spec_leaves_workspace_when_worktree_disabled() -> None:@@ -848,10 +854,13 @@ def test_enrich_team_spec_leaves_workspace_when_worktree_disabled() -> None:assert spec.agents["leader"].workspace is Noneassert spec.agents["teammate"].workspace is None+ # cwd is seeded regardless of worktree isolation.+ assert spec.agents["leader"].cwd == "/tmp/project"+ assert spec.agents["teammate"].cwd == "/tmp/project"def test_enrich_team_spec_preserves_explicit_member_workspace() -> None:- """A configured member workspace is not overwritten by project_dir."""+ """A configured member workspace survives; only cwd is seeded."""spec = _make_team_spec()spec.worktree = WorktreeConfig(enabled=True)spec.agents["leader"].workspace = WorkspaceSpec(root_path="/tmp/custom")@@ -865,7 +874,9 @@ def test_enrich_team_spec_preserves_explicit_member_workspace() -> None:)assert spec.agents["leader"].workspace.root_path == "/tmp/custom"- assert spec.agents["teammate"].workspace.root_path == "/tmp/project"+ assert spec.agents["teammate"].workspace is None+ assert spec.agents["leader"].cwd == "/tmp/project"+ assert spec.agents["teammate"].cwd == "/tmp/project"def test_enrich_team_spec_appends_after_existing_rails(monkeypatch) -> None: