config_runtime_keys.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. """Config keys the runtime reads that the DEFAULT_CONFIG walk in ``_validate_config_key`` cannot see.
  2. ``_validate_config_key`` asks here only AFTER the schema walk refused a key, so nothing here can
  3. override a schema verdict; it only vouches for an exact path (and what is below it) a reader uses.
  4. Four owners supply them:
  5. * Core selection keys (``browser.cloud_provider``, ``<category>.use_gateway``, ``stt.provider``):
  6. :func:`tools.tool_backend_helpers.selection_config_keys`, built from ``read_selection``'s tables.
  7. * Core-owned unseeded keys outside that table, each listed with its reader (:data:`CORE_RUNTIME_KEYS`).
  8. * MoA preset fields under any preset name (:func:`hermes_cli.moa_config.is_preset_field_path`). Only a
  9. preset's own fields and its aggregator slot fields qualify, not arbitrary descendants of ``moa.presets``.
  10. * ACTIVE plugins: a ``config_keys`` list in ``plugin.yaml`` (or ``extensions.com.nousresearch.hermes``
  11. in a portable ``plugin.json``) names core-section paths the plugin's code reads, e.g.
  12. ``memory.retaindb``. Plugins are found and gated by the loader's own discovery
  13. (``plugins_discovery``), plus the memory provider ``memory.provider`` selects. A declared path is
  14. at least two segments deep, is ignored when DEFAULT_CONFIG already defines it, and covers itself
  15. and everything below it.
  16. Nothing is cached: the lookup runs only for a key the schema refused, and a plugin installed or
  17. enabled in a long-running process must count on the next ``config get/set``.
  18. """
  19. from __future__ import annotations
  20. import logging
  21. from collections.abc import Iterable, Iterator
  22. logger = logging.getLogger(__name__)
  23. # Core-owned keys read at runtime but deliberately absent from DEFAULT_CONFIG -> their reader.
  24. CORE_RUNTIME_KEYS: dict[str, str] = {
  25. "computer_use.backend": "plugins/computer_use/__init__.py configured_backend_name() (unset = cua)",
  26. "dashboard.kanban": "plugins/kanban/dashboard/plugin_api.py (bundled dashboard tab preferences)",
  27. }
  28. def _covered(key: str, paths: Iterable[str]) -> bool:
  29. return any(key == path or key.startswith(path + ".") for path in paths)
  30. def _in_default_config(path: str) -> bool:
  31. from hermes_cli.config import DEFAULT_CONFIG
  32. node = DEFAULT_CONFIG
  33. for segment in path.split("."):
  34. if not isinstance(node, dict) or segment not in node:
  35. return False
  36. node = node[segment]
  37. return True
  38. def _selected_memory_provider_manifest():
  39. """Manifest of the memory provider ``memory.provider`` selects. Memory providers are ``exclusive``
  40. plugins: the general gate never enables them, the selection does."""
  41. from hermes_cli.config import cfg_get, load_config
  42. from hermes_cli.plugins_manifest import parse_manifest_file
  43. name = cfg_get(load_config(), "memory", "provider")
  44. if not isinstance(name, str) or not name.strip():
  45. return None
  46. from plugins.memory import _explicitly_disabled, find_provider_dir
  47. provider_dir = find_provider_dir(name.strip())
  48. # The memory loader refuses a provider parked in ``plugins.disabled``; its keys are not live either.
  49. if provider_dir is None or _explicitly_disabled(name.strip(), provider_dir):
  50. return None
  51. manifest_file = next((f for f in (provider_dir / "plugin.yaml", provider_dir / "plugin.yml") if f.exists()), None)
  52. return parse_manifest_file(manifest_file, provider_dir, "memory", "memory") if manifest_file else None
  53. def _active_manifests() -> Iterator:
  54. """Manifests of the plugins the loader would activate, by its own discovery, precedence and gate."""
  55. from hermes_cli.plugins_discovery import (
  56. _get_disabled_plugins, _get_enabled_plugins, collect_directory_manifests, discover_entrypoint_manifests,
  57. gate_manifest, resolve_manifest_winners,
  58. )
  59. from hermes_cli.plugins_manifest import manifest_key, requires_hermes_error
  60. manifests = collect_directory_manifests()
  61. # Same merge as PluginManager._discover_and_load_inner: a directory install keeps its identity.
  62. directory_keys = {manifest_key(m) for m in manifests}
  63. manifests += [m for m in discover_entrypoint_manifests() if manifest_key(m) not in directory_keys]
  64. disabled, enabled = _get_disabled_plugins(), _get_enabled_plugins()
  65. for manifest in resolve_manifest_winners(manifests).values():
  66. verdict = gate_manifest(manifest, disabled, enabled)
  67. if (verdict.action != "placeholder" or verdict.enabled) and requires_hermes_error(manifest) is None:
  68. yield manifest
  69. memory = _selected_memory_provider_manifest()
  70. if memory is not None:
  71. yield memory
  72. def plugin_declared_config_keys() -> frozenset[str]:
  73. """``config_keys`` declared by active plugins, minus paths DEFAULT_CONFIG already defines."""
  74. keys = set()
  75. for manifest in _active_manifests():
  76. for path in manifest.config_keys:
  77. if _in_default_config(path):
  78. logger.debug("config_keys: ignoring %s from plugin %s (a DEFAULT_CONFIG key)", path, manifest.name)
  79. else:
  80. keys.add(path)
  81. return frozenset(keys)
  82. def is_runtime_read_key(parts: list[str]) -> bool:
  83. """True when the split key ``parts`` is a MoA preset field, a core selection key, a core runtime key,
  84. or sits at/under a path an active plugin declares. Call only for a key the schema walk refused.
  85. The MoA rule needs the segments (a preset name may contain a dot); the other owners match the
  86. dotted join."""
  87. from hermes_cli.moa_config import is_preset_field_path
  88. from tools.tool_backend_helpers import selection_config_keys
  89. if is_preset_field_path(parts):
  90. return True
  91. dotted = ".".join(parts)
  92. if dotted in selection_config_keys() or _covered(dotted, CORE_RUNTIME_KEYS):
  93. return True
  94. return _covered(dotted, plugin_declared_config_keys())