hermes-agent/gateway/cwd_placeholder.py
infinitycrew39 1cc68fc897 fix(gateway): resolve terminal.cwd placeholders per backend and mount mode
Split placeholder TERMINAL_CWD resolution into three cases: local falls
back to MESSAGING_CWD/home; docker without workspace mount leaves cwd unset;
docker with mount enabled preserves an explicit host MESSAGING_CWD path for
terminal_tool's /workspace mapping. Stops leaking host Path.home() into
containers without breaking the mount contract.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-04 13:28:47 -07:00

49 lines
1.7 KiB
Python

"""Resolve gateway ``terminal.cwd`` placeholder values to ``TERMINAL_CWD``.
When ``terminal.cwd`` is unset or a placeholder (``.``, ``auto``, ``cwd``),
the gateway must not blindly map host ``Path.home()`` into container backends.
Docker with workspace mounting still needs an explicit host path signal
(``MESSAGING_CWD`` or an absolute config path) for ``terminal_tool`` to map
``/host/project`` → ``/workspace``.
"""
from __future__ import annotations
CWD_PLACEHOLDERS = frozenset({".", "auto", "cwd"})
def _truthy_env(value: str | None) -> bool:
return (value or "").strip().lower() in {"true", "1", "yes"}
def resolve_placeholder_terminal_cwd(
*,
configured_cwd: str,
terminal_backend: str,
messaging_cwd: str | None,
docker_mount_cwd_to_workspace: bool,
home_fallback: str,
) -> str | None:
"""Return the ``TERMINAL_CWD`` value to set, or ``None`` to leave it unset.
Cases:
- **local** + placeholder → ``MESSAGING_CWD`` or ``home_fallback``
- **docker** + placeholder + mount on + host ``MESSAGING_CWD`` → host path
(for ``terminal_tool`` ``/workspace`` mapping)
- **docker** + placeholder + mount off → ``None`` (sandbox default)
- other non-local backends + placeholder → ``None``
"""
if configured_cwd and configured_cwd not in CWD_PLACEHOLDERS:
return configured_cwd
backend = (terminal_backend or "local").strip().lower()
if backend == "local":
messaging = (messaging_cwd or "").strip()
return messaging or home_fallback
if backend == "docker" and docker_mount_cwd_to_workspace:
messaging = (messaging_cwd or "").strip()
if messaging and messaging not in CWD_PLACEHOLDERS:
return messaging
return None