-
Notifications
You must be signed in to change notification settings - Fork 52.9k
fix(gateway): translate Docker container paths for media delivery #42305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1024,6 +1024,57 @@ def _path_is_within(path: Path, root: Path) -> bool: | |
| return False | ||
|
|
||
|
|
||
| def _translate_docker_container_path(candidate: str) -> Optional[str]: | ||
| """Translate a Docker container path to its host-side equivalent. | ||
|
|
||
| When ``terminal.backend`` is ``docker``, the agent emits paths that | ||
| exist inside the container (e.g. ``/output/report.pdf``). The gateway | ||
| runs on the host and cannot resolve those paths directly. This helper | ||
| maps the container prefix to the host prefix using the longest-prefix | ||
| match against ``terminal.docker_volumes`` entries (``host:container`` | ||
| format). | ||
|
|
||
| Returns the translated host path string, or ``None`` if translation | ||
| is not applicable (backend is not docker, no matching volume, etc.). | ||
| """ | ||
| try: | ||
| from hermes_cli.config import load_config as _load_config | ||
| cfg = _load_config() | ||
| terminal_cfg = cfg.get("terminal", {}) | ||
| if not isinstance(terminal_cfg, dict): | ||
| return None | ||
| if terminal_cfg.get("backend") != "docker": | ||
| return None | ||
| volumes = terminal_cfg.get("docker_volumes", []) | ||
| if not isinstance(volumes, list): | ||
| return None | ||
| except Exception: | ||
| return None | ||
|
|
||
| # Longest-prefix match on container mount points. | ||
| best_match_len = 0 | ||
| best_host_prefix = None | ||
| for vol in volumes: | ||
| if not isinstance(vol, str) or ":" not in vol: | ||
| continue | ||
| host_part, _, container_part = vol.rpartition(":") | ||
| if not container_part or not host_part: | ||
| continue | ||
| container_prefix = container_part.rstrip("/") | ||
| if not container_prefix: | ||
| continue | ||
| if candidate == container_prefix or candidate.startswith(container_prefix + "/"): | ||
| if len(container_prefix) > best_match_len: | ||
| best_match_len = len(container_prefix) | ||
| best_host_prefix = host_part.rstrip("/") | ||
|
|
||
| if best_host_prefix is None: | ||
| return None | ||
|
|
||
| suffix = candidate[best_match_len:] | ||
| return best_host_prefix + suffix | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The suffix is unnormalized, so |
||
|
|
||
|
|
||
| def validate_media_delivery_path(path: str) -> Optional[str]: | ||
| """Return a safe absolute file path for native media delivery, else None. | ||
|
|
||
|
|
@@ -1062,6 +1113,17 @@ def validate_media_delivery_path(path: str) -> Optional[str]: | |
| if not expanded.is_absolute(): | ||
| return None | ||
|
|
||
| # When running inside a Docker sandbox, the agent emits container-local | ||
| # paths (e.g. /output/report.pdf) that don't exist on the host. | ||
| # Translate them to the host-side path using docker_volumes config | ||
| # before resolving. | ||
| translated = _translate_docker_container_path(str(expanded)) | ||
| if translated is not None: | ||
| try: | ||
| expanded = Path(translated) | ||
| except (OSError, RuntimeError, ValueError): | ||
| return None | ||
|
|
||
| try: | ||
| resolved = expanded.resolve(strict=True) | ||
| except (OSError, RuntimeError, ValueError): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This misparses supported
host:container[:options]specs:/host/data:/data:roproducescontainer_part == "ro", so paths below/datanever translate. Please parse the optional mode separately and add a:roregression test.