relay_cwd.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """Resolve logical working directories for Hermes-owned Relay scopes."""
  2. from __future__ import annotations
  3. import logging
  4. from typing import Any
  5. logger = logging.getLogger(__name__)
  6. _CWD_SENTINELS = frozenset({"", ".", "./", "auto", "cwd"})
  7. def _clean_cwd(value: Any) -> str:
  8. if not isinstance(value, str):
  9. return ""
  10. value = value.strip()
  11. return "" if value.lower() in _CWD_SENTINELS else value
  12. def _recorded_cwd(key: str) -> str:
  13. if not key:
  14. return ""
  15. from tools.terminal_tool import get_session_cwd
  16. return _clean_cwd(get_session_cwd(key))
  17. def resolve_relay_scope_cwds(
  18. agent: Any, task_id: str, session_id: str, platform: str
  19. ) -> tuple[str, str]:
  20. """Return logical ``(session_cwd, turn_cwd)`` for Relay scope input.
  21. A task may use a worktree distinct from its owning session. Preserve remote paths as
  22. declared and omit unknown paths instead of substituting the Hermes host's cwd.
  23. """
  24. try:
  25. task_cwd = _recorded_cwd(task_id)
  26. except Exception:
  27. logger.debug("Unable to read the Relay turn cwd", exc_info=True)
  28. task_cwd = ""
  29. session_cwd = ""
  30. try:
  31. from agent.runtime_cwd import scoped_session_cwd
  32. session_cwd = _clean_cwd(scoped_session_cwd())
  33. except Exception:
  34. logger.debug("Unable to read the scoped Relay session cwd", exc_info=True)
  35. if not session_cwd:
  36. try:
  37. from gateway.session_context import get_session_env
  38. session_key = get_session_env("HERMES_SESSION_KEY", "")
  39. for key in dict.fromkeys(key for key in (session_key, session_id) if key):
  40. if recorded := _recorded_cwd(key):
  41. session_cwd = recorded
  42. break
  43. except Exception:
  44. logger.debug("Unable to read the recorded Relay session cwd", exc_info=True)
  45. if not session_cwd:
  46. session_cwd = _clean_cwd(getattr(agent, "session_cwd", None))
  47. backend = ""
  48. if not session_cwd:
  49. try:
  50. from tools.terminal_scope import terminal_env
  51. backend = terminal_env("TERMINAL_ENV", "local").strip().lower()
  52. session_cwd = _clean_cwd(terminal_env("TERMINAL_CWD", ""))
  53. except Exception:
  54. logger.debug(
  55. "Unable to read the configured Relay session cwd", exc_info=True
  56. )
  57. if not session_cwd and platform in {"", "cli"} and backend in {"", "local"}:
  58. try:
  59. from agent.runtime_cwd import resolve_agent_cwd
  60. resolved = resolve_agent_cwd()
  61. session_cwd = _clean_cwd(
  62. str(resolved if resolved.is_absolute() else resolved.resolve())
  63. )
  64. except Exception:
  65. logger.debug("Unable to resolve the local Relay session cwd", exc_info=True)
  66. turn_cwd = task_cwd or session_cwd
  67. if platform in {"subagent", "cron"} and task_cwd:
  68. session_cwd = task_cwd
  69. return session_cwd or turn_cwd, turn_cwd