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: reuse path dispatch when building agent workspace

zhongqinghua25cb0b93c3a41
diff --git a/jiuwenswarm/common/utils.py b/jiuwenswarm/common/utils.py
index b1a04d10a8..d2d5e8c2da 100644
--- a/jiuwenswarm/common/utils.py
+++ b/jiuwenswarm/common/utils.py
@@ -1884,6 +1884,18 @@ def _dispatch_path(category, *, node=None, session_id=None, explicit=None):
return None
+def _dispatch_workspace_directories():
+ """Resolve Workspace directory nodes from the path provider, if any."""
+ provider = get_path_provider()
+ if provider is None:
+ return None
+ try:
+ return provider.build_workspace_directories(current_path_context())
+ except Exception:
+ logger.warning("path provider workspace build failed", exc_info=True)
+ return None
+
+
def get_agent_workspace_dir() -> Path:
"""Get the agent workspace directory path.
diff --git a/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py b/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py
index d0a590f60e..1463abfa5c 100644
--- a/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py
+++ b/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py
@@ -8787,27 +8787,22 @@ class JiuWenSwarmDeepAdapter:
return None
def _build_agent_workspace(self, *, language: str | None = None) -> Workspace:
- from jiuwenswarm.common.path_provider import (
- PathCategory, current_path_context, get_path_provider,
+ from jiuwenswarm.common.path_provider import PathCategory
+ from jiuwenswarm.common.utils import (
+ _dispatch_path,
+ _dispatch_workspace_directories,
)
root = self._workspace_dir or "./"
- directories = None
- provider = get_path_provider()
- if provider is not None:
- try:
- ctx = current_path_context()
- if self._workspace_dir is None:
- overridden = provider.resolve_path(PathCategory.WORKSPACE, ctx)
- if overridden is not None:
- root = str(overridden)
- directories = provider.build_workspace_directories(ctx)
- except Exception:
- logger.warning("path provider workspace build failed", exc_info=True)
+ overridden = _dispatch_path(
+ PathCategory.WORKSPACE, explicit=self._workspace_dir,
+ )
+ if overridden is not None:
+ root = str(overridden)
workspace = Workspace(
root_path=root, language=language or self._resolve_runtime_language(),
)
- for node in directories or []:
+ for node in _dispatch_workspace_directories() or []:
workspace.set_directory(node)
return workspace
diff --git a/tests/unit_tests/common/test_path_provider.py b/tests/unit_tests/common/test_path_provider.py
index fe55f2d6b4..a7f27d0e84 100644
--- a/tests/unit_tests/common/test_path_provider.py
+++ b/tests/unit_tests/common/test_path_provider.py
@@ -177,3 +177,53 @@ def test_workspace_directory_nodes_replace_defaults(tmp_path):
provider.build_workspace_directories(providers.current_path_context())
)
assert workspace.get_node_path("memory") == tmp_path / "state" / "memory"
+
+
+def test_dispatch_workspace_directories_uses_provider():
+ class Provider(providers.PathProvider):
+ name = "dirs"
+
+ def resolve_path(self, category, ctx, **kwargs):
+ return None
+
+ def build_workspace_directories(self, ctx):
+ return [
+ {
+ "name": "memory",
+ "description": "custom memory",
+ "path": "state/memory",
+ "children": [],
+ }
+ ]
+
+ providers.register_path_provider(Provider())
+ nodes = utils._dispatch_workspace_directories()
+ assert nodes[0]["path"] == "state/memory"
+
+
+def test_explicit_workspace_root_skips_provider_but_keeps_directory_nodes(tmp_path):
+ class Provider(providers.PathProvider):
+ name = "split"
+
+ def resolve_path(self, category, ctx, **kwargs):
+ if category == providers.PathCategory.WORKSPACE:
+ return tmp_path / "hijacked"
+ return None
+
+ def build_workspace_directories(self, ctx):
+ return [
+ {
+ "name": "memory",
+ "description": "",
+ "path": "state/memory",
+ "children": [],
+ }
+ ]
+
+ providers.register_path_provider(Provider())
+ kept = str(tmp_path / "kept")
+ assert utils._dispatch_path(
+ providers.PathCategory.WORKSPACE, explicit=kept,
+ ) is None
+ assert utils._dispatch_path(providers.PathCategory.WORKSPACE) == tmp_path / "hijacked"
+ assert utils._dispatch_workspace_directories()[0]["name"] == "memory"