profile_memory_config.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. """Carry the ACTIVE memory provider's own config into a ``--clone`` (#120115).
  2. ``--clone`` copies ``config.yaml`` — and with it ``memory.provider: hindsight`` — but the
  3. provider keeps its settings outside config.yaml, so the clone booted with the provider
  4. selected and silently unavailable. Providers store per-home config by convention (the same
  5. convention ``hermes_cli.web_routers.memory_providers`` reads): a ``<home>/<provider>/``
  6. directory (hindsight) or a flat ``<home>/<provider>.json`` (mem0, honcho, supermemory). Copying
  7. by convention keeps this free of plugin imports: the provider may live in the catalog, not in
  8. tree, so a hook the plugin must implement could not fix the reported case.
  9. """
  10. import contextlib
  11. import logging
  12. import os
  13. import re
  14. import shutil
  15. from pathlib import Path
  16. from typing import Callable, Optional
  17. from agent.memory_provider import MemoryProvider
  18. logger = logging.getLogger(__name__)
  19. # A provider name is a bare directory/file stem; anything else (path separators, ``..``, spaces)
  20. # would let a hand-edited config.yaml aim the copy outside the source profile.
  21. _PROVIDER_NAME_RE = re.compile(r"^[A-Za-z0-9_.-]+$")
  22. def active_memory_provider(config: Optional[dict]) -> Optional[str]:
  23. """The external ``memory.provider`` named in a parsed config.yaml, or None for the built-in
  24. store or an unsafe name."""
  25. from agent.memory_provider import is_core_memory_provider
  26. memory = (config or {}).get("memory")
  27. name = memory.get("provider") if isinstance(memory, dict) else None
  28. if not isinstance(name, str) or is_core_memory_provider(name):
  29. return None
  30. name = name.strip()
  31. if name in {".", ".."} or not _PROVIDER_NAME_RE.match(name):
  32. return None
  33. return name
  34. def clone_memory_provider_config(source_dir: Path, profile_dir: Path, provider: Optional[str]) -> bool:
  35. """Copy ``<provider>/`` and/or ``<provider>.json`` from *source_dir* into *profile_dir* when
  36. present. Files land owner-only like ``.env``: they can hold an API key. Returns True when
  37. anything was copied."""
  38. if not provider:
  39. return False
  40. copied = False
  41. src_dir = source_dir / provider
  42. if src_dir.is_dir():
  43. shutil.copytree(src_dir, profile_dir / provider, dirs_exist_ok=True)
  44. for root, _dirs, files in os.walk(profile_dir / provider):
  45. for filename in files:
  46. with contextlib.suppress(OSError):
  47. os.chmod(os.path.join(root, filename), 0o600)
  48. copied = True
  49. src_file = source_dir / f"{provider}.json"
  50. if src_file.is_file():
  51. dst = profile_dir / f"{provider}.json"
  52. shutil.copy2(src_file, dst)
  53. with contextlib.suppress(OSError):
  54. os.chmod(str(dst), 0o600)
  55. copied = True
  56. return copied
  57. def _load_config_dict(profile_dir: Path) -> Optional[dict]:
  58. """A profile's config.yaml mapping read through the config backend; None when absent or unreadable."""
  59. from hermes_yaml import YAMLError
  60. from hermes_cli.config_backend import read_config_doc
  61. try:
  62. data = read_config_doc(profile_dir / "config.yaml") or {}
  63. # FileNotFoundError included (no config). ValueError: the YAML constructors raise it bare for
  64. # values like ``2024-02-30`` / ``!!int abc``; this read is a best-effort notice and never fails a clone.
  65. except (YAMLError, OSError, ValueError):
  66. return None
  67. return data if isinstance(data, dict) else None
  68. def notify_memory_provider(profile_dir: Path, call: Callable[[MemoryProvider], None]) -> None:
  69. """Run ``call`` on the memory provider *profile_dir*'s config selects, with that profile's home AND
  70. its own secrets bound (a profile lifecycle hook): ``get_secret`` reads the target profile's
  71. ``.env`` and never falls back to the caller's environment for a key it lacks. No
  72. provider selected or installed: nothing runs. Best-effort: a failing
  73. provider, or an unreadable config, is logged and never fails the create/rename that called it
  74. (the directory is already published)."""
  75. from agent.secret_scope import build_profile_secret_scope, reset_secret_scope, set_secret_scope
  76. from hermes_constants import reset_hermes_home_override, set_hermes_home_override
  77. from plugins.memory import load_memory_provider
  78. name = None
  79. home_token = set_hermes_home_override(str(profile_dir))
  80. secret_token = None
  81. try:
  82. # Secrets first: the effective config (managed scope, ``${VAR}`` refs) expands through them.
  83. secret_token = set_secret_scope(build_profile_secret_scope(profile_dir), profile_home=str(profile_dir))
  84. from hermes_cli.config import load_config_readonly
  85. name = active_memory_provider(load_config_readonly())
  86. if not name:
  87. return
  88. provider = load_memory_provider(name, register_skills=False)
  89. if provider is not None:
  90. call(provider)
  91. except (Exception, SystemExit): # ConfigBackendUnavailable is a SystemExit; a plugin may sys.exit
  92. logger.warning("memory provider %r: profile hook failed for %s", name, profile_dir, exc_info=True)
  93. finally:
  94. if secret_token is not None:
  95. reset_secret_scope(secret_token)
  96. reset_hermes_home_override(home_token)
  97. def cloned_memory_provider(profile_dir: Path) -> Optional[str]:
  98. """Name of the external provider whose config *profile_dir* now carries, for the CLI notice."""
  99. provider = active_memory_provider(_load_config_dict(profile_dir))
  100. if provider and ((profile_dir / provider).is_dir() or (profile_dir / f"{provider}.json").is_file()):
  101. return provider
  102. return None