|
| 1 | +"""Boot-time helpers for resolving YAML-backed read-policy settings.""" |
| 2 | + |
| 3 | +from copy import deepcopy |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from app.boot.config import get_config_provider |
| 7 | + |
| 8 | + |
| 9 | +_DEFAULT_LIMITS = { |
| 10 | + "targeted": 8, |
| 11 | + "ambient": 12, |
| 12 | +} |
| 13 | +_DEFAULT_EXPAND = { |
| 14 | + "semantic_hops": 2, |
| 15 | + "include_problem_links": True, |
| 16 | + "include_fact_update_links": True, |
| 17 | + "include_association_links": True, |
| 18 | + "max_association_depth": 2, |
| 19 | + "min_association_strength": 0.25, |
| 20 | +} |
| 21 | +_DEFAULT_QUOTAS = { |
| 22 | + "targeted": {"direct": 4, "explicit": 3, "implicit": 1}, |
| 23 | + "ambient": {"direct": 4, "explicit": 5, "implicit": 3}, |
| 24 | +} |
| 25 | +_DEFAULT_RETRIEVAL = { |
| 26 | + "semantic_weight": 1.0, |
| 27 | + "keyword_weight": 1.0, |
| 28 | + "k_rrf": 20.0, |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +def get_read_settings() -> dict[str, Any]: |
| 33 | + """Return normalized read settings from YAML-backed runtime and policy config.""" |
| 34 | + |
| 35 | + config_provider = get_config_provider() |
| 36 | + read_policy = config_provider.get_read_policy() |
| 37 | + runtime = config_provider.get_runtime() |
| 38 | + cli_defaults = runtime.get("cli") or {} |
| 39 | + limits = read_policy.get("limits") or {} |
| 40 | + expansion = read_policy.get("expansion") or {} |
| 41 | + quotas = read_policy.get("quotas") or {} |
| 42 | + weights = read_policy.get("weights") or {} |
| 43 | + fusion = read_policy.get("fusion") or {} |
| 44 | + |
| 45 | + settings = { |
| 46 | + "default_mode": str(cli_defaults.get("default_mode", "targeted")), |
| 47 | + "include_global": bool(cli_defaults.get("include_global", True)), |
| 48 | + "limits_by_mode": { |
| 49 | + mode: int(limits.get(mode, fallback)) |
| 50 | + for mode, fallback in _DEFAULT_LIMITS.items() |
| 51 | + }, |
| 52 | + "expand": { |
| 53 | + "semantic_hops": int(expansion.get("semantic_hops", _DEFAULT_EXPAND["semantic_hops"])), |
| 54 | + "include_problem_links": bool( |
| 55 | + expansion.get("include_problem_links", _DEFAULT_EXPAND["include_problem_links"]) |
| 56 | + ), |
| 57 | + "include_fact_update_links": bool( |
| 58 | + expansion.get("include_fact_update_links", _DEFAULT_EXPAND["include_fact_update_links"]) |
| 59 | + ), |
| 60 | + "include_association_links": bool( |
| 61 | + expansion.get("include_association_links", _DEFAULT_EXPAND["include_association_links"]) |
| 62 | + ), |
| 63 | + "max_association_depth": int( |
| 64 | + expansion.get("max_association_depth", _DEFAULT_EXPAND["max_association_depth"]) |
| 65 | + ), |
| 66 | + "min_association_strength": float( |
| 67 | + expansion.get("min_association_strength", _DEFAULT_EXPAND["min_association_strength"]) |
| 68 | + ), |
| 69 | + }, |
| 70 | + "quotas_by_mode": { |
| 71 | + mode: { |
| 72 | + bucket: int((quotas.get(mode) or {}).get(bucket, fallback)) |
| 73 | + for bucket, fallback in defaults.items() |
| 74 | + } |
| 75 | + for mode, defaults in _DEFAULT_QUOTAS.items() |
| 76 | + }, |
| 77 | + "retrieval": { |
| 78 | + "semantic_weight": float(weights.get("semantic", _DEFAULT_RETRIEVAL["semantic_weight"])), |
| 79 | + "keyword_weight": float(weights.get("keyword", _DEFAULT_RETRIEVAL["keyword_weight"])), |
| 80 | + "k_rrf": float(fusion.get("k_rrf", _DEFAULT_RETRIEVAL["k_rrf"])), |
| 81 | + }, |
| 82 | + } |
| 83 | + return deepcopy(settings) |
| 84 | + |
| 85 | + |
| 86 | +def get_read_hydration_defaults() -> dict[str, Any]: |
| 87 | + """Return the read defaults expected by CLI hydration.""" |
| 88 | + |
| 89 | + settings = get_read_settings() |
| 90 | + return { |
| 91 | + "default_mode": settings["default_mode"], |
| 92 | + "include_global": settings["include_global"], |
| 93 | + "limits_by_mode": deepcopy(settings["limits_by_mode"]), |
| 94 | + "expand": deepcopy(settings["expand"]), |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | +def get_retrieval_defaults() -> dict[str, float]: |
| 99 | + """Return normalized retrieval defaults for fusion and seed retrieval.""" |
| 100 | + |
| 101 | + return dict(get_read_settings()["retrieval"]) |
| 102 | + |
| 103 | + |
| 104 | +def resolve_read_limit(*, mode: str, explicit_limit: int | None) -> int: |
| 105 | + """Resolve the effective read limit from explicit payload or mode-based config.""" |
| 106 | + |
| 107 | + if explicit_limit is not None: |
| 108 | + return int(explicit_limit) |
| 109 | + settings = get_read_settings() |
| 110 | + return int(settings["limits_by_mode"].get(mode, _DEFAULT_LIMITS["targeted"])) |
| 111 | + |
| 112 | + |
| 113 | +def resolve_read_quotas(*, mode: str) -> dict[str, int]: |
| 114 | + """Resolve the configured context-pack quotas for one read mode.""" |
| 115 | + |
| 116 | + settings = get_read_settings() |
| 117 | + quotas = settings["quotas_by_mode"].get(mode, settings["quotas_by_mode"]["targeted"]) |
| 118 | + return {bucket: int(value) for bucket, value in quotas.items()} |
0 commit comments