jobs_model_pin.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536
  1. """The main agent's current ``(provider, model)``, which ``pinned=True`` locks onto a cron job."""
  2. from __future__ import annotations
  3. import contextlib
  4. from typing import Optional
  5. from hermes_constants import get_hermes_home
  6. def _main_model_pin() -> tuple[Optional[str], Optional[str]]:
  7. """``(provider, model)`` the main agent runs on right now (``model.default`` + the provider it
  8. resolves to), for ``pinned=True`` jobs: the lock is a plain per-job pin, so the scheduler needs
  9. no second precedence axis. ``(None, None)`` when nothing is configured (the job stays unpinned).
  10. ``ValueError`` when the config backend cannot serve the profile (``ConfigBackendUnavailable``):
  11. answering "nothing configured" would silently drop the lock the caller asked for, so the
  12. create/update is refused like any other invalid input, and the caller's process lives on."""
  13. from cron.jobs import _normalize_job_optional_text # late: the facade imports this module
  14. from hermes_cli.config_backend import ConfigBackendUnavailable, config_exists
  15. from hermes_cli.config_effective import load_user_config_effective
  16. cfg_path = get_hermes_home() / "config.yaml"
  17. try:
  18. cfg = load_user_config_effective(cfg_path) if config_exists(cfg_path) else {}
  19. except ConfigBackendUnavailable as exc:
  20. raise ValueError(f"cannot pin the job to the main model: {exc.code}") from exc
  21. model_cfg = cfg.get("model") or {}
  22. model = model_cfg.get("default") or model_cfg.get("model") if isinstance(model_cfg, dict) else model_cfg
  23. model = _normalize_job_optional_text(model)
  24. if not model:
  25. return None, None
  26. provider = None
  27. with contextlib.suppress(Exception):
  28. from hermes_cli.runtime_provider import resolve_runtime_provider
  29. provider = _normalize_job_optional_text(resolve_runtime_provider(requested=None).get("provider"))
  30. return (provider.lower() if provider else None), model