Skip to content

Commit 96edefb

Browse files
committed
fix: update sandbox shell environment variable handling
1 parent dae4397 commit 96edefb

1 file changed

Lines changed: 21 additions & 15 deletions

File tree

apps/application/flow/backend/sandbox_shell.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,32 @@
44

55
from deepagents.backends import LocalShellBackend
66
from deepagents.backends.protocol import ExecuteResponse
7-
87
from maxkb.const import CONFIG
98

10-
_enable_sandbox = bool(int(CONFIG.get('SANDBOX', 0)))
11-
_run_user = 'sandbox' if _enable_sandbox else getpass.getuser()
12-
_sandbox_python_sys_path = CONFIG.get_sandbox_python_package_paths().replace(',', ':')
9+
_enable_sandbox = bool(int(CONFIG.get("SANDBOX", 0)))
10+
_run_user = "sandbox" if _enable_sandbox else getpass.getuser()
11+
_sandbox_python_sys_path = CONFIG.get_sandbox_python_package_paths().replace(",", ":")
1312

1413

1514
class SandboxShellBackend(LocalShellBackend):
1615
def __init__(self, root_dir: str, **kwargs):
17-
if 'env' not in kwargs and not kwargs.get('inherit_env', False):
16+
if "env" not in kwargs and not kwargs.get("inherit_env", False):
1817
env = os.environ.copy()
19-
path = env.get('PATH', '/usr/bin:/bin')
18+
python_path = env.get("PYTHONPATH", "")
2019

21-
# 将 sandbox 路径分解为列表,检查每个路径是否已存在
22-
existing_paths = set(path.split(os.pathsep))
20+
# 将 sandbox Python 包路径分解为列表,检查每个路径是否已存在
21+
existing_paths = set(python_path.split(os.pathsep))
2322
sandbox_paths = _sandbox_python_sys_path.split(os.pathsep) if _sandbox_python_sys_path else []
2423
new_paths = [p for p in sandbox_paths if p and p not in existing_paths]
2524

2625
if new_paths:
27-
env['PATH'] = f"{os.pathsep.join(new_paths)}{os.pathsep}{path}"
26+
env["PYTHONPATH"] = (
27+
f"{os.pathsep.join(new_paths)}{os.pathsep}{python_path}"
28+
if python_path
29+
else os.pathsep.join(new_paths)
30+
)
2831

29-
kwargs['env'] = env
32+
kwargs["env"] = env
3033
super().__init__(root_dir=root_dir, **kwargs)
3134

3235
def _translate_virtual_paths(self, command: str) -> str:
@@ -51,18 +54,21 @@ def translate(m: re.Match) -> str:
5154
return re.sub(r'(?<![.\w\-])/[A-Za-z_][^\s\'"\\;|&><:,]*', translate, command)
5255

5356
def execute(
54-
self,
55-
command: str,
56-
*,
57-
timeout: int | None = None,
57+
self,
58+
command: str,
59+
*,
60+
timeout: int | None = None,
5861
) -> ExecuteResponse:
5962
if self.virtual_mode:
6063
command = self._translate_virtual_paths(command)
6164

6265
if _enable_sandbox:
6366
# 用 runuser 在子进程里切换用户,父进程凭据保持不变,
6467
# 避免父进程 ruid/euid 不一致导致 execve 报 Permission denied
65-
command = f"env -i LD_PRELOAD=/opt/maxkb-app/sandbox/lib/sandbox.so PATH=${{PATH}} gosu {_run_user} {command}"
68+
command = (
69+
"env -i LD_PRELOAD=/opt/maxkb-app/sandbox/lib/sandbox.so "
70+
f'PATH="${{PATH}}" PYTHONPATH="${{PYTHONPATH}}" gosu {_run_user} {command}'
71+
)
6672
# command = f"runuser -u {_run_user} -- env -i PATH=${{PATH}} {command}"
6773

6874
# print(f"Executing command in sandbox: {command}")

0 commit comments

Comments
 (0)