config_effective.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. """The effective USER config: config.yaml + managed overlay + ``${VAR}`` expansion, no defaults.
  2. ``load_config()`` merges ``DEFAULT_CONFIG`` first, which is wrong for readers that treat a
  3. missing key as "unset" (the gateway's presence-sensitive env bridge, ``cfg == {}`` sentinels,
  4. cron model pinning) — so nine surfaces used to hand-roll raw-read → overlay → expand in
  5. differing orders and none of them replayed the model-key canonicalization or the last-known-good
  6. recovery ``load_config()`` gained. This module is that one primitive.
  7. Order matches ``_load_config_impl``: the user layer is expanded BEFORE the managed overlay so a
  8. managed ``${VAR}`` resolves against the process environment only (``apply_managed_overlay``
  9. expands it) and can never be re-resolved through a profile's secret scope
  10. (docs/design/managed-scope.md §4.1). ``read_user_config_raw`` stays the write-back primitive.
  11. """
  12. from __future__ import annotations
  13. import copy
  14. from pathlib import Path
  15. from typing import Any, Dict, Optional, Tuple
  16. from hermes_cli import config as _config
  17. from hermes_cli import managed_scope
  18. from hermes_cli.config_backend import read_config_doc
  19. from hermes_cli.config_read_errors import _warn_config_parse_failure
  20. # path -> raw user mapping from the last successful parse in this process; served (through the
  21. # normal pipeline) when the file is later found mid-edit as broken YAML.
  22. _LAST_GOOD_USER_RAW: dict[str, dict[str, Any]] = {}
  23. # path -> (*user_signature, *managed_signature, effective, env_snapshot); see utils.file_signature.
  24. _EFFECTIVE_CACHE: dict[str, tuple[Any, ...]] = {}
  25. def _effective(raw: dict[str, Any]) -> dict[str, Any]:
  26. expanded = _config._expand_env_vars(raw)
  27. merged = managed_scope.apply_managed_overlay(expanded if isinstance(expanded, dict) else {})
  28. return _config._normalize_root_model_keys(merged if isinstance(merged, dict) else {})
  29. def _recover_user_raw(config_path: Path, path_key: str, exc: Exception) -> dict[str, Any]:
  30. """Last-known-good raw user mapping after a parse failure: this process's last good parse,
  31. else the newest ``good`` copy in backups/config/, else ``{}`` (warned as defaults)."""
  32. raw = _LAST_GOOD_USER_RAW.get(path_key)
  33. fallback = "last-known-good"
  34. if raw is None:
  35. from hermes_cli.config_backups import load_newest_good_backup
  36. raw = load_newest_good_backup(config_path)
  37. fallback = "last-known-good-backup"
  38. _warn_config_parse_failure(config_path, exc, fallback=fallback if raw is not None else "defaults")
  39. return copy.deepcopy(raw) if raw is not None else {}
  40. def load_user_config_effective(config_path: Optional[Path] = None, *, fail_closed: bool = False) -> dict[str, Any]:
  41. """User ``config.yaml`` → ``${VAR}`` expansion → managed overlay → model-key canonicalization.
  42. NO ``DEFAULT_CONFIG`` merge: a key absent from the file (and from the managed layer) is absent
  43. here, so ``{}`` sentinels and presence-sensitive bridges keep working. An absent file is an
  44. empty user layer (the managed layer still applies). Returns a fresh deepcopy.
  45. Broken YAML: ``fail_closed=True`` raises the parse error (for callers that keep their own
  46. last-good state); otherwise the last successfully parsed user file — in-process first, then
  47. the newest ``backups/config/*.good.*`` copy — is served through the same pipeline, so a
  48. mid-edit torn write never silently drops user overrides (same contract as ``load_config``).
  49. Cached on the user + managed file signatures and the values of every referenced env var."""
  50. from agent.safe_worker_policy import worker_config_snapshot
  51. # A frozen-policy worker is detached from the profile's files: the owner-supplied
  52. # snapshot IS the user layer (same seam load_config / read_raw_config honour).
  53. snapshot = worker_config_snapshot()
  54. if snapshot is not None:
  55. return _effective(snapshot)
  56. if config_path is None:
  57. config_path = _config.get_config_path()
  58. path_key = str(config_path)
  59. with _config._CONFIG_LOCK:
  60. user_sig, cache_sig = _config._load_config_cache_sig(config_path)
  61. cached = _EFFECTIVE_CACHE.get(path_key)
  62. n = len(cache_sig) if cache_sig is not None else 0
  63. if cached is not None and cache_sig is not None and cached[:n] == cache_sig:
  64. if all(_config._env_ref_lookup(k) == v for k, v in cached[n + 1].items()):
  65. return copy.deepcopy(cached[n])
  66. raw: dict[str, Any] = {}
  67. recovered = False
  68. raw_hit = _config._raw_config_cache_hit(path_key, user_sig) if user_sig is not None else None
  69. if raw_hit is not None:
  70. raw = copy.deepcopy(raw_hit) # one parse per process, shared with read_raw_config()
  71. _LAST_GOOD_USER_RAW.setdefault(path_key, copy.deepcopy(raw))
  72. elif user_sig is not None:
  73. try:
  74. loaded = read_config_doc(config_path)
  75. except Exception as exc:
  76. if fail_closed:
  77. raise
  78. raw, recovered = _recover_user_raw(config_path, path_key, exc), True
  79. else:
  80. raw = loaded if isinstance(loaded, dict) else {}
  81. _config._raw_config_cache_store(path_key, user_sig, copy.deepcopy(raw))
  82. _LAST_GOOD_USER_RAW[path_key] = copy.deepcopy(raw)
  83. # Same copy load_config keeps: a fresh process recovers from it (see _recover_user_raw).
  84. # Only for the ACTIVE home — a read of another profile's file (doctor, TUI cwd lookup)
  85. # must not create backups/ inside that profile.
  86. if config_path == _config.get_config_path():
  87. from hermes_cli.config_backups import backup_config
  88. backup_config(config_path, "good")
  89. env_snapshot = _config._env_ref_snapshot(raw)
  90. managed = managed_scope.load_managed_config()
  91. if managed:
  92. _config._env_ref_snapshot(managed, env_snapshot)
  93. effective = _effective(raw)
  94. # A recovered result is never cached under the corrupt file's signature: a later
  95. # ``fail_closed`` caller must still see the parse error, not a cache hit.
  96. if cache_sig is not None and not recovered:
  97. _EFFECTIVE_CACHE[path_key] = (*cache_sig, copy.deepcopy(effective), env_snapshot)
  98. else:
  99. _EFFECTIVE_CACHE.pop(path_key, None)
  100. return effective