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: 信任工作空间注入的路径信息不再写入用户消息

CodehubTTGd7d266a84479
diff --git a/jiuwenswarm/server/runtime/agent_adapter/interface.py b/jiuwenswarm/server/runtime/agent_adapter/interface.py
index c18f27b25f..e96a132b5e 100644
--- a/jiuwenswarm/server/runtime/agent_adapter/interface.py
+++ b/jiuwenswarm/server/runtime/agent_adapter/interface.py
@@ -1364,6 +1364,9 @@ class JiuWenSwarm:
for d in raw_trusted_dirs:
if isinstance(d, str) and d.strip():
trusted_dirs.append(d.strip())
+ # 显式传入快照:仅这部分进用户消息;开关兜底注入只服务权限轨,不进用户消息,
+ # 避免模型把亲缘路径当越界、用聊天文本代替系统审批卡。
+ explicit_trusted_dirs = list(trusted_dirs)
# 用户选中的 skill 名列表(前端从 content 提取,如 /doc /review)。
# 若提供,build_user_prompt 直接用作 skills_to_use、且不剥离 content。
skills: list[str] | None = None
@@ -1380,7 +1383,8 @@ class JiuWenSwarm:
if isinstance(metadata_project_dir, str) and metadata_project_dir.strip()
else None
)
- # 未显式传 trusted_dirs 时,若 workspace.read 已开为 allow 则注入 project_dir
+ # 未显式传 trusted_dirs 时,若 workspace.read 已开为 allow 则为权限轨注入 project_dir。
+ # 仅注入权限轨(file_guard 免审批),不进用户消息。
if not trusted_dirs and project_dir:
try:
ws_trusted = get_permissions_file_guard_workspace_access().get("read") == "allow"
@@ -1438,7 +1442,7 @@ class JiuWenSwarm:
files=params.get("files", {}),
channel=channel,
language=language,
- trusted_dirs=trusted_dirs,
+ trusted_dirs=explicit_trusted_dirs,
metadata=request.metadata,
skills=skills,
supplementary_info=supplementary,
@@ -1449,7 +1453,7 @@ class JiuWenSwarm:
files=params.get("files", {}),
channel=channel,
language=language,
- trusted_dirs=trusted_dirs,
+ trusted_dirs=explicit_trusted_dirs,
metadata=request.metadata,
skills=skills,
supplementary_info=supplementary,
diff --git a/tests/unit_tests/agentserver/test_agentserver_modes.py b/tests/unit_tests/agentserver/test_agentserver_modes.py
index d40a49158d..6e05c91c05 100644
--- a/tests/unit_tests/agentserver/test_agentserver_modes.py
+++ b/tests/unit_tests/agentserver/test_agentserver_modes.py
@@ -435,6 +435,7 @@ def test_build_inputs_injects_project_dir_as_trusted_when_workspace_read_allow(m
)
asyncio.run(interface_module.JiuWenSwarm().process_message(request))
assert fake_adapter.seen_inputs["trusted_dirs"] == [os.path.abspath("/tmp/project")]
+ assert '"trusted_dirs"' not in fake_adapter.seen_inputs["query"]
def test_build_inputs_skips_injection_when_workspace_read_ask(monkeypatch):
@@ -469,6 +470,7 @@ def test_build_inputs_preserves_explicit_trusted_dirs_even_when_read_ask(monkeyp
)
asyncio.run(interface_module.JiuWenSwarm().process_message(request))
assert fake_adapter.seen_inputs["trusted_dirs"] == ["/tmp/explicit"]
+ assert '"trusted_dirs"' in fake_adapter.seen_inputs["query"]
def test_build_inputs_skips_injection_when_workspace_access_raises(monkeypatch):
@@ -488,6 +490,28 @@ def test_build_inputs_skips_injection_when_workspace_access_raises(monkeypatch):
assert "trusted_dirs" not in fake_adapter.seen_inputs
+def test_build_inputs_explicit_trusted_dirs_still_in_user_message_when_read_allow(monkeypatch):
+ interface_module, fake_adapter = _prepare_build_inputs_trusted_dirs_test(monkeypatch)
+ monkeypatch.setattr(
+ interface_module,
+ "get_permissions_file_guard_workspace_access",
+ lambda: {"read": "allow", "write": "allow", "exec": "allow"},
+ )
+ request = AgentRequest(
+ request_id="req-explicit-allow",
+ channel_id="tui",
+ session_id="tui_session",
+ params={
+ "query": "hello",
+ "project_dir": "/tmp/project",
+ "trusted_dirs": ["/tmp/explicit"],
+ },
+ )
+ asyncio.run(interface_module.JiuWenSwarm().process_message(request))
+ assert fake_adapter.seen_inputs["trusted_dirs"] == ["/tmp/explicit"]
+ assert '"trusted_dirs"' in fake_adapter.seen_inputs["query"]
+
+
def test_build_inputs_propagates_user_interaction_capability(monkeypatch):
from jiuwenswarm.server.runtime.agent_adapter import interface as interface_module