Skip to content
← How we build

backend/src/router/config_loader.py

"""Load routing YAML from the repository config directory."""

from __future__ import annotations

import logging
from functools import lru_cache
from pathlib import Path
from typing import Any

import yaml

from config.paths import resolve_repo_path
from config.settings import get_settings
from observability import log_event

logger = logging.getLogger("agentic.request")
trace_logger = logging.getLogger("agentic.trace")


def routing_config_dir() -> Path:
    relative = get_settings().routing_config_dir
    resolved = resolve_repo_path(relative)
    trace_logger.debug(
        "routing_config_dir relative=%s resolved=%s",
        relative,
        resolved,
    )
    return resolved


@lru_cache
def load_thresholds() -> dict[str, Any]:
    path = routing_config_dir() / "thresholds.yaml"
    log_event(
        "thresholds_load",
        component="ConfigLoader",
        fields={"path": str(path)},
    )
    with path.open(encoding="utf-8") as handle:
        data = yaml.safe_load(handle)
    result = data or {}
    trace_logger.debug(
        "routing_thresholds_loaded keys=%s",
        sorted(result.keys()) if isinstance(result, dict) else type(result).__name__,
    )
    return result


@lru_cache
def load_routes() -> list[dict[str, Any]]:
    path = routing_config_dir() / "skills_routing.yaml"
    log_event(
        "routes_load",
        component="ConfigLoader",
        fields={"path": str(path)},
    )
    with path.open(encoding="utf-8") as handle:
        data = yaml.safe_load(handle)
    routes = list((data or {}).get("routes", []))
    route_ids = [route.get("id") for route in routes if isinstance(route, dict)]
    log_event(
        "routes_loaded",
        component="ConfigLoader",
        fields={"route_count": len(routes)},
    )
    trace_logger.debug("routing_routes_detail route_ids=%s", route_ids)
    return routes

Follow the work.

Occasional updates on SignalFoundry, MarketCompass, and what we are building at CompassFoundry Labs.

No spam. Unsubscribe anytime.