config_env_routing.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """Which ``hermes config`` keys live in ``.env`` instead of ``config.yaml``, and their lifecycle.
  2. Platform setting keys such as ``FEISHU_HOME_CHANNEL`` had two writers: the platform setup flows and
  3. ``/sethome`` persist them to ``.env`` through ``save_env_value``, while ``hermes config set`` only
  4. routed credential-shaped names there and wrote every other bare name to the top level of
  5. ``config.yaml``. The gateway bridges top-level scalars into the environment only when ``.env`` lacks
  6. the name and one-shot CLI readers never bridge, so the two copies diverged silently (#111848).
  7. The routing rule is the key's SHAPE, not a registry: a bare ``UPPER_SNAKE`` name is an environment
  8. setting and goes to ``.env`` — the file every runtime reader (``os.getenv``, the gateway's
  9. ``platform_gate_env``) resolves against — whether or not Hermes enumerates it anywhere. Roughly 290
  10. of the ~700 documented variables (``TELEGRAM_GROUP_ALLOWED_USERS``, ``HERMES_TIMEZONE``, ...) are
  11. read straight from the environment without being registered in ``OPTIONAL_ENV_VARS``, so a registry
  12. check alone kept landing them in ``config.yaml``. Provider credentials keep their own rotation
  13. lifecycle in ``hermes_cli.credential_lifecycle``.
  14. """
  15. import re
  16. import sys
  17. from pathlib import Path
  18. from typing import Optional
  19. # Environment-variable shape: what every shell and ``os.getenv`` caller treats as a variable name.
  20. # Case-sensitive on purpose: a lowercase bare name (``my_flag``) stays a config.yaml top-level key.
  21. _ENV_SHAPE_RE = re.compile(r"^[A-Z][A-Z0-9_]*$")
  22. def is_registered_env_name(name: str) -> bool:
  23. """True when Hermes itself enumerates ``name``: ``OPTIONAL_ENV_VARS`` / ``_EXTRA_ENV_KEYS``, or a
  24. self-configuring platform suffix so plugin adapters nobody listed (``IRC_HOME_CHANNEL``) count."""
  25. from hermes_cli.config import _EXTRA_ENV_KEYS, OPTIONAL_ENV_VARS
  26. from hermes_cli.setup_hidden_env import is_setup_hidden_env
  27. return name in OPTIONAL_ENV_VARS or name in _EXTRA_ENV_KEYS or is_setup_hidden_env(name)
  28. def is_env_setting_key(key: str) -> bool:
  29. """True for a bare (undotted) key ``hermes config`` stores in ``.env``: any ``UPPER_SNAKE`` name,
  30. plus registered names typed in any case (``discord_home_channel``)."""
  31. if "." in key:
  32. return False
  33. return bool(_ENV_SHAPE_RE.match(key)) or is_registered_env_name(key.upper())
  34. def _drop_config_yaml_copies(key: str) -> bool:
  35. """Remove same-named top-level ``config.yaml`` copies (as typed and upper-cased) so the ``.env``
  36. value is the only one the gateway bridge and CLI readers can disagree about."""
  37. from hermes_cli.config import _write_user_config, get_config_path, require_readable_config_before_write
  38. config_path = get_config_path()
  39. user_config = require_readable_config_before_write(config_path)
  40. stale = [name for name in {key, key.upper()} if name in user_config]
  41. for name in stale:
  42. del user_config[name]
  43. if stale:
  44. _write_user_config(config_path, user_config)
  45. return bool(stale)
  46. def save_env_setting(key: str, value: str) -> None:
  47. from hermes_cli.config import require_env_writable, save_env_value
  48. require_env_writable(key.upper(), "set")
  49. save_env_value(key.upper(), value)
  50. _drop_config_yaml_copies(key)
  51. def remove_env_setting(key: str) -> bool:
  52. """Remove the ``.env`` entry and any stale ``config.yaml`` copy; False when neither existed."""
  53. from hermes_cli.config import remove_env_value, require_env_writable
  54. require_env_writable(key.upper(), "remove")
  55. removed = remove_env_value(key.upper())
  56. return _drop_config_yaml_copies(key) or removed
  57. def read_env_setting(key: str) -> Optional[str]:
  58. """Resolve like the gateway does: ``.env`` first, then a not-yet-converged top-level
  59. ``config.yaml`` copy under the name as typed, which is reported as stale on stderr."""
  60. from hermes_cli.config import get_env_value, read_raw_config_readonly
  61. value = get_env_value(key.upper())
  62. if value is None:
  63. value = read_raw_config_readonly().get(key)
  64. if value is not None:
  65. print(f" (note: {key} is a stale top-level config.yaml copy; `hermes config set {key} <value>` "
  66. f"moves it to .env, `hermes config unset {key}` removes it)", file=sys.stderr)
  67. return value