auxiliary_task_config.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """Read-time resolution of an ``auxiliary.<task>`` config block.
  2. Built-in slot defaults come from ``DEFAULT_CONFIG``; plugin-registered tasks
  3. (``PluginContext.register_auxiliary_task``) layer their declared ``defaults``
  4. under the user's block and may inherit another slot via ``inherit_from``.
  5. """
  6. from __future__ import annotations
  7. import contextlib
  8. import logging
  9. from contextvars import ContextVar
  10. from typing import Any, Dict
  11. logger = logging.getLogger(__name__)
  12. # A bypass session's (``--ignore-user-config``, safe mode) own saved ``auxiliary:`` section (code defaults
  13. # only), set by its ``policy_scope`` so its side tasks never read the profile. None (the default) reads
  14. # config.yaml.
  15. _SESSION_AUXILIARY: ContextVar = ContextVar("session_auxiliary", default=None)
  16. @contextlib.contextmanager
  17. def session_auxiliary_scope(auxiliary):
  18. token = _SESSION_AUXILIARY.set(auxiliary if isinstance(auxiliary, dict) else {})
  19. try:
  20. yield
  21. finally:
  22. _SESSION_AUXILIARY.reset(token)
  23. class AuthoritativeAuxiliaryTask(str):
  24. """A task name that keeps the task's non-route policy (timeout, concurrency, extra_body, fallback
  25. chain) but strips its route fields, so a one-shot fallback naming a complete route never inherits
  26. an unset endpoint/key/wire mode from the route that just failed (#113322, #130895)."""
  27. __slots__ = ()
  28. route_authoritative = True
  29. def authoritative_auxiliary_task(task: str | None) -> str:
  30. return task if isinstance(task, AuthoritativeAuxiliaryTask) else AuthoritativeAuxiliaryTask(str(task or ""))
  31. def _get_auxiliary_task_config(task: str, _seen: frozenset = frozenset()) -> dict[str, Any]:
  32. """Config dict for auxiliary.<task>, or {} when unavailable. Plugin-registered tasks get their
  33. declared defaults layered under user config (user wins); built-in defaults live in DEFAULT_CONFIG.
  34. A task registered with ``inherit_from`` is resolved here, at read time, over the base task's
  35. effective config, so it follows the base's current settings (and the active profile's config)
  36. until the user pins a route on the task itself. ``_seen`` guards re-registration cycles."""
  37. if not task:
  38. return {}
  39. config = _task_config(str(task), _seen)
  40. if getattr(task, "route_authoritative", False):
  41. return {key: value for key, value in config.items() if key not in _AUX_ROUTE_KEYS}
  42. return config
  43. def _task_config(task: str, _seen: frozenset) -> dict[str, Any]:
  44. aux = _SESSION_AUXILIARY.get()
  45. if aux is None:
  46. try:
  47. from hermes_cli.config import load_config_readonly
  48. config = load_config_readonly()
  49. except ImportError:
  50. return {}
  51. aux = config.get("auxiliary", {}) if isinstance(config, dict) else {}
  52. task_config = aux.get(task, {}) if isinstance(aux, dict) else {}
  53. if not isinstance(task_config, dict):
  54. task_config = {}
  55. try:
  56. from hermes_cli.plugins import get_plugin_auxiliary_tasks
  57. for _entry in get_plugin_auxiliary_tasks():
  58. if _entry.get("key") == task:
  59. _defaults = _entry.get("defaults") or {}
  60. if isinstance(_defaults, dict):
  61. _inherit = _entry.get("inherit_from")
  62. if _inherit and task in _seen:
  63. logger.warning("Auxiliary task %r has a circular inherit_from chain — "
  64. "ignoring inheritance", task)
  65. if not _inherit or task in _seen:
  66. return {**_defaults, **task_config}
  67. base = _get_auxiliary_task_config(_inherit, _seen | {task})
  68. return _layer_over_inherited({**base, **_defaults}, task_config)
  69. break
  70. except Exception: # health: allow BLE001 -- plugin discovery must never break aux config reads
  71. logger.debug("plugin auxiliary task lookup failed for %r", task, exc_info=True)
  72. return task_config
  73. # The fields that together pick WHERE a call goes. They travel as one unit: a provider pinned on an
  74. # inheriting task must never pick up the base's base_url/api_key (that would send one vendor's key
  75. # to another's endpoint).
  76. _AUX_ROUTE_KEYS = frozenset({"provider", "model", "base_url", "api_key", "api_mode", "key_env",
  77. "api_key_env", "reasoning_effort"})
  78. def _layer_over_inherited(inherited: dict[str, Any], user: dict[str, Any]) -> dict[str, Any]:
  79. """Merge a task's own ``auxiliary.<task>`` block over its inherited base.
  80. The picker, "reset to auto" and the dashboard persist ``provider: auto`` plus ``""`` for
  81. model/base_url/api_key/reasoning_effort when the operator expresses no preference, so those
  82. placeholders mean "follow the base", not "override it with nothing". Once the operator pins a
  83. route (a non-auto provider, a model or a base_url) the whole route comes from the task's own
  84. block. Non-route keys (timeout, extra_body, ...) override per key; only ``""`` is dropped,
  85. never other falsy values (``reasoning_effort: false`` is an explicit choice)."""
  86. provider = str(user.get("provider") or "").strip().lower()
  87. pinned = (provider not in ("", "auto") or bool(str(user.get("model") or "").strip())
  88. or bool(str(user.get("base_url") or "").strip()))
  89. merged = {k: v for k, v in inherited.items() if not (pinned and k in _AUX_ROUTE_KEYS)}
  90. for key, value in user.items():
  91. if value == "" and not (pinned and key in _AUX_ROUTE_KEYS):
  92. continue
  93. if key == "provider" and not pinned:
  94. continue
  95. merged[key] = value
  96. return merged