The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): support Docker /workspace media paths in gateway delivery
CharZhoua7dd88543934
diff --git a/gateway/platforms/base.py b/gateway/platforms/base.pyindex 2b73a3dc1f4..b8fe6e4e705 100644--- a/gateway/platforms/base.py+++ b/gateway/platforms/base.py@@ -1460,6 +1460,134 @@ def _path_is_within(path: Path, root: Path) -> bool:return False+def _parse_docker_volume_mounts() -> List[Tuple[Path, Path]]:+ """Parse configured Docker volume mounts into ``(host_path, container_path)``.++ Source of truth is ``TERMINAL_DOCKER_VOLUMES`` (JSON list of+ ``host:container[:mode]`` specs), matching terminal/docker runtime config.+ Named volumes and non-absolute hosts are skipped because they cannot be+ resolved on the gateway host for media delivery.+ """+ raw = os.getenv("TERMINAL_DOCKER_VOLUMES", "").strip()+ if not raw:+ return []+ try:+ import json as _json++ parsed = _json.loads(raw)+ except Exception:+ return []+ if not isinstance(parsed, list):+ return []++ mounts: List[Tuple[Path, Path]] = []+ for entry in parsed:+ if not isinstance(entry, str):+ continue+ spec = entry.strip()+ if not spec:+ continue+ # Prefer the first ':/' so absolute container paths are unambiguous.+ sep = spec.find(":/")+ if sep <= 0:+ continue+ host_raw = spec[:sep]+ container_and_mode = spec[sep + 1 :] # starts with /+ container_raw = container_and_mode.split(":", 1)[0]+ if not container_raw.startswith("/"):+ continue+ # Skip named volumes (no absolute/drive host path).+ host_expanded = os.path.expanduser(host_raw)+ if not (+ host_expanded.startswith("/")+ or (len(host_expanded) > 1 and host_expanded[1] == ":")+ ):+ continue+ try:+ host_path = Path(host_expanded).resolve(strict=False)+ container_path = Path(container_raw)+ except (OSError, RuntimeError, ValueError):+ continue+ if not container_path.is_absolute():+ continue+ mounts.append((host_path, container_path))+ return mounts+++def _default_docker_workspace_host_root() -> Optional[Path]:+ """Host path for Docker's default persistent ``/workspace`` mount."""+ if os.getenv("TERMINAL_ENV", "").strip().lower() != "docker":+ return None+ if os.getenv("TERMINAL_CONTAINER_PERSISTENT", "true").strip().lower() not in {+ "1",+ "true",+ "yes",+ "on",+ }:+ return None+ # Explicit cwd mount takes over /workspace when enabled.+ if os.getenv("TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACE", "false").strip().lower() in {+ "1",+ "true",+ "yes",+ "on",+ }:+ cwd = os.getenv("TERMINAL_CWD") or os.getcwd()+ try:+ host = Path(os.path.expanduser(cwd)).resolve(strict=False)+ except (OSError, RuntimeError, ValueError):+ return None+ return host if host.is_dir() else None+ try:+ from tools.environments.base import get_sandbox_dir++ root = (get_sandbox_dir() / "docker" / "default" / "workspace").resolve(strict=False)+ except Exception:+ return None+ return root if root.is_dir() else None+++def _translate_docker_container_media_path(candidate: Path) -> Optional[Path]:+ """Translate a container-absolute path to its host path when possible.++ Uses longest-prefix match across configured ``docker_volumes``, then falls+ back to the default persistent Docker ``/workspace`` host root.+ """+ if not candidate.is_absolute():+ return None++ mounts = list(_parse_docker_volume_mounts())+ # Synthetic /workspace mount for default persistent sandbox / cwd bind.+ default_ws = _default_docker_workspace_host_root()+ if default_ws is not None and not any(c.as_posix() == "/workspace" for _, c in mounts):+ mounts.append((default_ws, Path("/workspace")))++ if not mounts:+ return None++ # Longest container-prefix match.+ best: Optional[Tuple[Path, Path, int]] = None+ candidate_posix = candidate.as_posix()+ for host_root, container_root in mounts:+ container_posix = container_root.as_posix().rstrip("/") or "/"+ if candidate_posix == container_posix or candidate_posix.startswith(container_posix + "/"):+ score = len(container_posix)+ if best is None or score > best[2]:+ best = (host_root, container_root, score)+ if best is None:+ return None++ host_root, container_root, _ = best+ try:+ relative = candidate.relative_to(container_root)+ translated = (host_root / relative).resolve(strict=True)+ except (OSError, RuntimeError, ValueError):+ return None+ if translated != host_root and not _path_is_within(translated, host_root):+ return None+ return translated++def validate_media_delivery_path(path: str) -> Optional[str]:"""Return a safe absolute file path for native media delivery, else None.@@ -1498,10 +1626,17 @@ def validate_media_delivery_path(path: str) -> Optional[str]:if not expanded.is_absolute():return None- try:- resolved = expanded.resolve(strict=True)- except (OSError, RuntimeError, ValueError):- return None+ # Docker agents emit MEDIA:/workspace/... (or other configured container+ # mount paths). Resolve those to host paths before the normal host-side+ # existence / denylist checks.+ translated = _translate_docker_container_media_path(expanded)+ if translated is not None:+ resolved = translated+ else:+ try:+ resolved = expanded.resolve(strict=True)+ except (OSError, RuntimeError, ValueError):+ return Noneif not resolved.is_file():return Nonediff --git a/tests/gateway/test_platform_base.py b/tests/gateway/test_platform_base.pyindex 8f781f47549..e6743d4a403 100644--- a/tests/gateway/test_platform_base.py+++ b/tests/gateway/test_platform_base.py@@ -781,6 +781,85 @@ class TestMediaDeliveryDefaultMode:assert BasePlatformAdapter.validate_media_delivery_path(str(link)) is None+class TestDockerContainerMediaPathTranslation:+ """MEDIA:/workspace (and configured mounts) must resolve to host paths."""++ def test_configured_workspace_mount_translates(self, tmp_path, monkeypatch):+ import json++ host_ws = tmp_path / "host-ws"+ host_ws.mkdir()+ media = host_ws / "shot.png"+ media.write_bytes(b"\x89PNG\r\n\x1a\n")+ monkeypatch.setenv(+ "TERMINAL_DOCKER_VOLUMES",+ json.dumps([f"{host_ws}:/workspace"]),+ )+ monkeypatch.delenv("TERMINAL_ENV", raising=False)++ assert BasePlatformAdapter.validate_media_delivery_path(+ "/workspace/shot.png"+ ) == str(media.resolve())++ def test_configured_output_mount_translates(self, tmp_path, monkeypatch):+ import json++ host_out = tmp_path / "documents"+ host_out.mkdir()+ media = host_out / "report.pdf"+ media.write_bytes(b"%PDF-1.4")+ monkeypatch.setenv(+ "TERMINAL_DOCKER_VOLUMES",+ json.dumps([f"{host_out}:/output"]),+ )++ assert BasePlatformAdapter.validate_media_delivery_path(+ "/output/report.pdf"+ ) == str(media.resolve())++ def test_longest_prefix_wins(self, tmp_path, monkeypatch):+ import json++ host_a = tmp_path / "a"+ host_b = tmp_path / "b"+ host_a.mkdir()+ host_b.mkdir()+ nested = host_b / "file.png"+ nested.write_bytes(b"png")+ monkeypatch.setenv(+ "TERMINAL_DOCKER_VOLUMES",+ json.dumps([+ f"{host_a}:/data",+ f"{host_b}:/data/nested",+ ]),+ )++ assert BasePlatformAdapter.validate_media_delivery_path(+ "/data/nested/file.png"+ ) == str(nested.resolve())++ def test_default_persistent_workspace_fallback(self, tmp_path, monkeypatch):+ sandbox = tmp_path / "sandboxes"+ ws = sandbox / "docker" / "default" / "workspace"+ ws.mkdir(parents=True)+ media = ws / "out.png"+ media.write_bytes(b"png")+ monkeypatch.setenv("TERMINAL_ENV", "docker")+ monkeypatch.setenv("TERMINAL_CONTAINER_PERSISTENT", "true")+ monkeypatch.setenv("TERMINAL_SANDBOX_DIR", str(sandbox))+ monkeypatch.delenv("TERMINAL_DOCKER_VOLUMES", raising=False)+ monkeypatch.delenv("TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACE", raising=False)++ assert BasePlatformAdapter.validate_media_delivery_path(+ "/workspace/out.png"+ ) == str(media.resolve())++ def test_unmapped_container_path_fails(self, monkeypatch):+ monkeypatch.delenv("TERMINAL_DOCKER_VOLUMES", raising=False)+ monkeypatch.delenv("TERMINAL_ENV", raising=False)+ assert BasePlatformAdapter.validate_media_delivery_path("/workspace/nope.png") is None++# ---------------------------------------------------------------------------# should_send_media_as_audio# ---------------------------------------------------------------------------