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:修cpython路径探测

lby3f10d53f3536
diff --git a/jiuwenbox/src/jiuwenbox/server/runtime/process.py b/jiuwenbox/src/jiuwenbox/server/runtime/process.py
index e58e4a9c58..9bc3e69cac 100644
--- a/jiuwenbox/src/jiuwenbox/server/runtime/process.py
+++ b/jiuwenbox/src/jiuwenbox/server/runtime/process.py
@@ -160,6 +160,27 @@ else:
PYTHON_EXECUTABLE = "python3"
+def _resolve_local_jiuwenbox_src_dir() -> Path | None:
+ """从 sys.path 反推仓库内 jiuwenbox 源码目录 (``<repo>/jiuwenbox/src``).
+
+ dev 场景 agent-server 不 pip install jiuwenbox, 而是 spawn box-server 时
+ 注入 ``PYTHONPATH=<repo>/jiuwenbox/src`` (经 Popen env block, 不回写
+ os.environ). box-server 内该路径已进 sys.path, 由此反推: 找到包含
+ ``jiuwenbox/__init__.py`` 的 sys.path 条目, 返回其父目录 (即 ``src``).
+ 返回 None 表示未找到 (site-packages 安装版/冻包, runner python 自带模块).
+ """
+ for entry in sys.path:
+ if not entry:
+ continue
+ try:
+ candidate = Path(entry) / "jiuwenbox" / "__init__.py"
+ if candidate.is_file():
+ return candidate.parent.parent
+ except OSError:
+ continue
+ return None
+
+
def _alloc_loopback_port() -> int:
"""分配一个空闲 TCP loopback 端口 (OS 自动选, bind 后立即 close).
@@ -3133,6 +3154,16 @@ class ProcessRuntime(RuntimeAdapter):
_inherit_val = os.environ.get(_inherit_key)
if _inherit_val:
env[_inherit_key] = _inherit_val
+ # PYTHONPATH 兜底: agent-server 用 ``Popen(env=...)`` 拉起 box-server 时,
+ # ``PYTHONPATH=<repo>/jiuwenbox/src`` 只进了 spawn env block, 不在 box-server
+ # 的 os.environ 里 (env dict 不回写父进程变量), 上面的继承拿不到. 此时从
+ # sys.path 反推: box-server 能 import jiuwenbox 说明源码路径在 sys.path 中,
+ # 把它拼成 PYTHONPATH 传给 runner, 避免系统 runner python (site-packages
+ # 未装 jiuwenbox) 因 ModuleNotFoundError 启动即退出.
+ if not env.get("PYTHONPATH") and not getattr(sys, "frozen", False):
+ _src_dir = str(_resolve_local_jiuwenbox_src_dir())
+ if _src_dir:
+ env["PYTHONPATH"] = _src_dir
for _k, _v in os.environ.items():
if _k.startswith("JIUWENBOX_") and _k not in env and _v:
env[_k] = _v
diff --git a/jiuwenswarm/server/agent_ws_server.py b/jiuwenswarm/server/agent_ws_server.py
index 81c5471682..89a6ad0194 100644
--- a/jiuwenswarm/server/agent_ws_server.py
+++ b/jiuwenswarm/server/agent_ws_server.py
@@ -145,6 +145,7 @@ from jiuwenswarm.common.config import (
update_sandbox_runtime,
upsert_mcp_server_in_config,
)
+from jiuwenswarm.server.sandbox_config_rpc import get_sandbox_config_req_methods
from jiuwenswarm.server.sandbox.jiuwenbox_runner import JiuwenBoxRunner
from jiuwenswarm.common.security.ws_origin import (
extract_handshake_request,
@@ -179,6 +180,26 @@ async def _warm_interface_deep_module() -> None:
)
+def _is_std_cpython(python_exe: str) -> bool:
+ """判断 python.exe 是否标准 CPython 安装 (非 venv trampoline/launcher).
+
+ jbx-sandbox 跑不了 uv trampoline/venv launcher (WinError 5), runner 必须用
+ 标准 CPython. 判定: 同目录有 python3*.dll (CPython 根目录特征), 且不在
+ .../Scripts/ 子目录 (venv 的 python.exe 在 Scripts/ 中, 无 python3*.dll).
+ """
+ p = Path(python_exe)
+ try:
+ if not p.is_file():
+ return False
+ except OSError:
+ return False
+ parent = p.parent
+ if parent.name.lower() == "scripts":
+ return False
+ has_dll = any(parent.glob("python3*.dll"))
+ return has_dll
+
+
async def ensure_interface_deep_and_checkpointer() -> None:
"""Make interface_deep importable without a synchronous import on this task.
@@ -585,17 +606,33 @@ class AgentWebSocketServer:
产品起不来, 也无从修复)。
"""
try:
+ # 非 Linux/Windows 平台直接跳过 auto-start: jiuwenbox 依赖平台专属
+ # 内核能力 (Linux: bwrap/Landlock/命名空间; Windows: win_setup 用户
+ # 创建 + WFP + ACL), 其它平台 (macOS 等) 起不来; 即便 spawn 成功后续
+ # /sandbox 命令也会被 :func:`_require_sandbox_supported` 拒掉, 留着
+ # 只会浪费一次失败的子进程启动。
+ if not (sys.platform.startswith("linux") or sys.platform == "win32"):
+ logger.info(
+ "[sandbox_lifecycle] skipping jiuwenbox auto-start: "
+ "/sandbox is only supported on Linux/Windows (current: %r)",
+ sys.platform,
+ )
+ return
# sandbox.enabled 门控: false 时整个跳过 (不拉起 box-server, 不触发
# install/创建 jbx-sandbox 用户)。优先级高于 startup_mode — enabled=false
# 即使用户配了 startup_mode=internal 也不拉起。默认 false: shipped 模板
# enabled=false, 用户不显式写 sandbox.enabled: true 就不拉起 (opt-in,
# 避免开箱即 install + 建进程)。
- if not get_sandbox_runtime().get("enabled"):
- logger.info(
- "[AgentWebSocketServer] sandbox.enabled=false, "
- "skipping jiuwenbox auto-start",
- )
- return
+ if sys.platform == "win32":
+ from jiuwenswarm.common.config import get_sandbox_runtime
+ sandbox_runtime = get_sandbox_runtime()
+ if not sandbox_runtime.get("enabled"):
+ logger.info(
+ "[sandbox_lifecycle] sandbox.enabled=false, "
+ "skipping jiuwenbox auto-start (不拉起 box-server, 不创建沙箱用户). "
+ "如需沙箱, 在 config.yaml 设 sandbox.enabled: true + startup_mode: internal"
+ )
+ return
explicit_mode = get_sandbox_startup_mode_explicit()
if explicit_mode is None:
logger.info(
@@ -640,6 +677,31 @@ class AgentWebSocketServer:
)
return
+ # Windows: policy = 打包基底 (windows-policy.yaml) + workspace 副本
+ # (windows-policy.runtime.yaml) 合并, 由 box-server PolicyReader.load_policy
+ # 做 (机制对齐 config.yaml template+override). 副本路径经
+ # JIUWENBOX_POLICY_PATH 注入 (基底由 box-server 自己解析). 副本不存在则
+ # 建空骨架. Linux 不走 windows-policy.
+ if sys.platform == "win32":
+ try:
+ from jiuwenswarm.server.sandbox_policy_render import (
+ ensure_copy_exists,
+ )
+ runtime_policy = ensure_copy_exists()
+ if runtime_policy is not None and runtime_policy.is_file():
+ policy_path = runtime_policy
+ logger.info(
+ "[sandbox_lifecycle] using runtime policy copy: %s "
+ "(box-server merges base + copy)",
+ policy_path,
+ )
+ except Exception as exc: # noqa: BLE001
+ logger.warning(
+ "[sandbox_lifecycle] ensure runtime copy failed, "
+ "fall back to base policy: %s",
+ exc,
+ )
+
from jiuwenswarm.server.handlers.sandbox import (
allocate_internal_jiuwenbox_port,
parse_sandbox_host_port,
@@ -658,11 +720,97 @@ class AgentWebSocketServer:
port,
)
+ # 注入动态路径 env 给 box-server 子进程 (Windows 沙箱用):
+ # JIUWENBOX_BUNDLED_PYTHON / JIUWENBOX_VENV_DIR / JIUWENBOX_RUNNER_PYTHON
+ # (runner 用的标准 CPython, 非 uv venv — jbx-sandbox 跑不了 uv trampoline).
+ # P0-5: 不写 os.environ (主进程全局污染), 改构建 sandbox_env dict 给
+ # ensure_running(extra_env=...) 传子进程. 仅 Windows 需要.
+ sandbox_env: dict[str, str] = {}
+ if sys.platform == "win32":
+ try:
+ from jiuwenswarm.server.runtime.pip_env import (
+ ensure_runtime_venv, resolve_base_python,
+ )
+ try:
+ bundled_python = resolve_base_python()
+ sandbox_env["JIUWENBOX_BUNDLED_PYTHON"] = str(bundled_python.parent)
+ sandbox_env["JIUWENCLAW_BASE_PYTHON"] = str(bundled_python)
+ except Exception as exc: # noqa: BLE001
+ logger.warning(
+ "[sandbox_lifecycle] inject JIUWENBOX_BUNDLED_PYTHON failed: %s",
+ exc,
+ )
+ try:
+ venv_dir = ensure_runtime_venv()
+ sandbox_env["JIUWENBOX_VENV_DIR"] = str(venv_dir)
+ except Exception as exc: # noqa: BLE001
+ logger.warning(
+ "[sandbox_lifecycle] inject JIUWENBOX_VENV_DIR failed: %s",
+ exc,
+ )
+ except Exception as exc: # noqa: BLE001
+ logger.warning(
+ "[sandbox_lifecycle] inject sandbox python/venv env failed: %s",
+ exc,
+ )
+ for _py_key in ("CLAW_PYTHON_HOME", "JIUWENCLAW_BASE_PYTHON"):
+ _py_val = (os.environ.get(_py_key) or "").strip()
+ if _py_val and not sandbox_env.get(_py_key):
+ sandbox_env[_py_key] = _py_val
+ _desktop_data = (os.environ.get("JIUWENBOX_DESKTOP_DATA_DIR") or "").strip()
+ if _desktop_data:
+ sandbox_env["JIUWENBOX_DESKTOP_DATA_DIR"] = _desktop_data
+ if getattr(sys, "frozen", False):
+ sandbox_env["JIUWENBOX_RUNNER_PYTHON"] = str(Path(sys.executable).resolve())
+ elif not (sandbox_env.get("JIUWENBOX_RUNNER_PYTHON")
+ or os.environ.get("JIUWENBOX_RUNNER_PYTHON") or "").strip():
+ logger.info(
+ "[sandbox_lifecycle] JIUWENBOX_RUNNER_PYTHON "
+ "未注入, 探测候选路径..."
+ )
+ import shutil as _shutil
+ import glob as _glob
+ _runner_py: str | None = None
+ _candidates: list[str] = []
+ _candidates.append(
+ str(Path(__file__).resolve().parents[2] / "tools" / "python" / "python.exe"))
+ _candidates += sorted(_glob.glob(r"C:\Python3*\python.exe"))
+ _lad = os.environ.get("LOCALAPPDATA", "")
+ if _lad:
+ _candidates += sorted(_glob.glob(
+ str(Path(_lad) / "Programs" / "Python" / "Python3*" / "python.exe")))
+ # uv 管理的标准 CPython (非 Scripts trampoline)
+ _roaming = os.environ.get("APPDATA", "")
+ if _roaming:
+ _candidates += sorted(_glob.glob(
+ str(Path(_roaming) / "uv" / "python" / "cpython-*" / "python.exe")))
+ if sys.executable and _is_std_cpython(sys.executable):
+ _candidates.insert(0, sys.executable)
+ for _cand in _candidates:
+ if _cand and Path(_cand).is_file() and _is_std_cpython(_cand):
+ _runner_py = str(Path(_cand).resolve())
+ break
+ if not _runner_py:
+ _which = _shutil.which("python") or _shutil.which("python3")
+ if _which and _is_std_cpython(_which):
+ _runner_py = str(Path(_which).resolve())
+ if _runner_py:
+ sandbox_env["JIUWENBOX_RUNNER_PYTHON"] = _runner_py
+ logger.info(
+ "[sandbox_lifecycle] injected env: "
+ "JIUWENBOX_VENV_DIR=%s, JIUWENBOX_BUNDLED_PYTHON=%s, "
+ "JIUWENBOX_RUNNER_PYTHON=%s",
+ sandbox_env.get("JIUWENBOX_VENV_DIR") or "<未注入>",
+ sandbox_env.get("JIUWENBOX_BUNDLED_PYTHON") or "<未注入>",
+ sandbox_env.get("JIUWENBOX_RUNNER_PYTHON") or "<未注入>",
+ )
ok = await self._jiuwenbox_runner.ensure_running(
host=host,
port=port,
startup_mode="internal",
policy_path=policy_path,
+ extra_env=sandbox_env or None,
+ timeout=180.0,
)
if not ok:
stderr_tail = self._jiuwenbox_runner.get_stderr_tail(10)
@@ -967,6 +1115,41 @@ class AgentWebSocketServer:
)
await dispatch_parsed_request(ctx, request, peer=ws)
+ if request.req_method in get_sandbox_config_req_methods():
+ logger.info(
+ f"[AgentWebSocketServer] 处理 sandbox.config: request_id={request.request_id}",
+ extra={"user_visible": "progress"},
+ )
+ await self._handle_sandbox_config(ws, request, send_lock)
+
+ async def _handle_sandbox_config(self, ws: Any, request: AgentRequest, send_lock: asyncio.Lock) -> None:
+ """处理 sandbox.* 配置 E2A 请求 (officeAce 经 WS 控制沙箱开关/启动方式/文件/网络)."""
+ from jiuwenswarm.server.sandbox_config_rpc import dispatch_sandbox_config_request
+ from jiuwenswarm.common.schema.message import ReqMethod
+
+ resp = dispatch_sandbox_config_request(request)
+ wire = encode_agent_response_for_wire(resp, response_id=request.request_id)
+ async with send_lock:
+ await send_wire_payload(ws, wire)
+
+ # 沙箱开关变更后必须 reload_agent_config, 否则同 session 已活着的
+ # ReActAgent 仍持有旧 SANDBOX sysop id (已被 teardown 从 resource_mgr
+ # 移除), 后续工具调用拿到 None sysop 表现为无权限.
+ # 新 session 不受影响: 它懒创建时直接读新 enabled 值造 LOCAL sysop.
+ if resp.ok and request.req_method == ReqMethod.SANDBOX_ENABLED_SET:
+ try:
+ config_base = get_config()
+ await self._agent_manager.reload_agents_config(config_base, None)
+ logger.info(
+ "[AgentWebSocketServer] sandbox.enabled.set 后触发 reload_agents_config, "
+ "同 session ReActAgent 将切到新 sysop",
+ )
+ except Exception as exc: # noqa: BLE001
+ logger.warning(
+ "[AgentWebSocketServer] sandbox.enabled.set 后 reload_agents_config 失败: %s",
+ exc,
+ )
+
@staticmethod
async def _trigger_before_ws_server_start_hook() -> None:
"""在首次启动之前触发扩展;未初始化 ExtensionRegistry 时跳过。"""
diff --git a/jiuwenswarm/server/sandbox_policy_render.py b/jiuwenswarm/server/sandbox_policy_render.py
index f73d9dfccc..1059d5ab72 100644
--- a/jiuwenswarm/server/sandbox_policy_render.py
+++ b/jiuwenswarm/server/sandbox_policy_render.py
@@ -93,7 +93,7 @@ def _empty_skeleton() -> dict[str, Any]:
}
-def _ensure_copy_exists() -> Path:
+def ensure_copy_exists() -> Path:
"""副本不存在时建稀疏空骨架 (不 dump 基底). 返回副本路径.
拷贝只一次: 已存在不重建 (exe 重启 / box-server 重启不重拷). 基底内容由
@@ -116,7 +116,7 @@ def _ensure_copy_exists() -> Path:
def _load_copy() -> dict[str, Any]:
"""读副本 (不存在则建空骨架并返回)."""
with _copy_lock: # P1-13: 串行化, 防与 _save_copy 并发 lost-update
- p = _ensure_copy_exists()
+ p = ensure_copy_exists()
try:
data = yaml.safe_load(p.read_text(encoding="utf-8"))
except (OSError, yaml.YAMLError) as exc: