cwd_placeholder.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. """Resolve gateway ``terminal.cwd`` placeholder values to ``TERMINAL_CWD``.
  2. When ``terminal.cwd`` is unset or a placeholder (``.``, ``auto``, ``cwd``),
  3. the gateway must not blindly map host ``Path.home()`` into container backends.
  4. Docker with workspace mounting still needs an explicit host path signal
  5. (``MESSAGING_CWD`` or an absolute config path) for ``terminal_tool`` to map
  6. ``/host/project`` → ``/workspace``.
  7. """
  8. from __future__ import annotations
  9. CWD_PLACEHOLDERS = frozenset({".", "auto", "cwd"})
  10. def resolve_placeholder_terminal_cwd(
  11. *,
  12. configured_cwd: str,
  13. terminal_backend: str,
  14. messaging_cwd: str | None,
  15. docker_mount_cwd_to_workspace: bool,
  16. home_fallback: str,
  17. ) -> str | None:
  18. """Return the ``TERMINAL_CWD`` value to set, or ``None`` to leave it unset.
  19. local + placeholder → ``MESSAGING_CWD`` or ``home_fallback``; docker +
  20. placeholder + mount on + host ``MESSAGING_CWD`` → that host path (for the
  21. ``/workspace`` mapping); any other non-local backend → ``None`` (sandbox default).
  22. """
  23. if configured_cwd and configured_cwd not in CWD_PLACEHOLDERS:
  24. return configured_cwd
  25. backend = (terminal_backend or "local").strip().lower()
  26. messaging = (messaging_cwd or "").strip()
  27. if backend == "local":
  28. return messaging or home_fallback
  29. if backend == "docker" and docker_mount_cwd_to_workspace and messaging and messaging not in CWD_PLACEHOLDERS:
  30. return messaging
  31. return None