-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
115 lines (93 loc) · 3.89 KB
/
Copy pathconfig.py
File metadata and controls
115 lines (93 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""Global Configuration Management for Ariel-Memory.
Handles environment-based settings, feature flags, and hook orchestration
using a singleton pattern to ensure consistent state across the server.
Config resolution order:
1. $MCP_CONFIG_PATH (per-agent overrides — one config per data dir)
2. repo-root config.yaml (shared default)
When a per-agent copy lacks keys present in the repo default (e.g. after
an update ships new settings), a startup warning lists them — defaults
still apply, but the copy is worth refreshing.
"""
import logging
import os
import threading
from pathlib import Path
from typing import Any, Self
import yaml
logger = logging.getLogger(__name__)
def _missing_keys(loaded: dict[str, Any], reference: dict[str, Any], prefix: str = "") -> list[str]:
"""Keys present in reference but absent from loaded (deep)."""
missing: list[str] = []
for key, value in reference.items():
full = f"{prefix}{key}"
if key not in loaded:
missing.append(full)
elif isinstance(value, dict) and isinstance(loaded.get(key), dict):
missing.extend(_missing_keys(loaded[key], value, prefix=full + "."))
return missing
class Config:
_instance: Self | None = None
_data: dict[str, Any] = {}
_lock = threading.Lock()
def __new__(cls) -> Self:
with cls._lock:
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance._load()
return cls._instance
@staticmethod
def config_path() -> Path:
"""Per-agent config wins; repo-root config.yaml is the shared default."""
override = os.environ.get("MCP_CONFIG_PATH")
if override:
p = Path(override)
if p.is_file():
return p
return Path(__file__).parent / "config.yaml"
def _load(self) -> None:
path = self.config_path()
try:
with open(path) as f:
self._data = yaml.safe_load(f) or {}
except FileNotFoundError:
self._data = {}
self._warn_on_drift(path)
def _warn_on_drift(self, path: Path) -> None:
"""Warn when a per-agent copy lacks keys added by newer defaults."""
repo_default = Path(__file__).parent / "config.yaml"
try:
if not path.is_file() or path.resolve() == repo_default.resolve():
return
with open(repo_default) as f:
reference = yaml.safe_load(f) or {}
missing = _missing_keys(self._data, reference)
if missing:
logger.warning(
"Config %s is missing %d key(s) present in the repo default (defaults still apply): %s — consider refreshing from config.yaml",
path,
len(missing),
", ".join(missing[:10]),
)
except Exception:
logger.debug("config drift check failed", exc_info=True)
def get(self, *keys: str, default: Any = None) -> Any:
value = self._data
for key in keys:
if isinstance(value, dict):
value = value.get(key, default)
else:
return default
return value
def is_hook_enabled(self, layer: str, hook: str) -> bool:
"""Single source of truth: hooks.<layer>.<hook> in config.yaml.
Default True — a hook not mentioned in yaml runs; disable explicitly
with `false`. No code-side duplicate list to drift.
"""
return bool(self.get("hooks", layer, hook, default=True))
def is_feature_enabled(self, feature: str) -> bool:
return bool(self.get("features", feature, default=False))
def get_limit(self, key: str) -> int:
return int(self.get("limits", key, default=0))
def get_forgetting(self, key: str) -> float:
return float(self.get("forgetting", key, default=0.0))
config = Config()