Skip to content

AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).

perf(config): build settings once and rebuild only when their inputs change (#489)

Rufus04612b13edd9
diff --git a/packages/core/openexecutive/config.py b/packages/core/openexecutive/config.py
index ed6732d2..7de48d73 100644
--- a/packages/core/openexecutive/config.py
+++ b/packages/core/openexecutive/config.py
@@ -1,5 +1,7 @@
import logging
+import os
import re
+import threading
from pathlib import Path
from typing import Annotated, Any, Literal
@@ -1538,5 +1540,41 @@ def mcp_config_file_present(config_path: Path) -> bool:
return False
+# get_settings() is called hundreds of times a minute (every key decrypt,
+# every per-tenant check), and building Settings re-parses the environment
+# and .env for every field: ~5 ms each, most of an idle server's CPU. So it
+# is built once and rebuilt only when what it reads changes: the
+# environment, the working directory (relative path defaults), the .env
+# file, and whether the MCP config file exists (_resolve_mcp).
+_settings_lock = threading.Lock()
+_settings_cache: tuple[tuple[object, ...], bool, Settings] | None = None
+
+
+def _settings_key() -> tuple[object, ...]:
+ try:
+ stat = _ENV_FILE.stat()
+ env_file: tuple[int, int] | None = (stat.st_mtime_ns, stat.st_size)
+ except OSError:
+ env_file = None
+ try:
+ cwd: str | None = os.getcwd()
+ except OSError:
+ cwd = None
+ return (tuple(os.environ.items()), cwd, env_file)
+
+
def get_settings() -> Settings:
- return Settings() # type: ignore[call-arg]
+ """The current settings. Each call returns its own copy, so a caller that
+ changes a field never changes anyone else's."""
+ global _settings_cache
+ key = _settings_key()
+ with _settings_lock:
+ cached = _settings_cache
+ if cached is not None:
+ cached_key, mcp_file, settings = cached
+ if cached_key == key and mcp_config_file_present(settings.mcp_servers_config_path) == mcp_file:
+ return settings.model_copy(deep=True)
+ settings = Settings() # type: ignore[call-arg]
+ with _settings_lock:
+ _settings_cache = (key, mcp_config_file_present(settings.mcp_servers_config_path), settings)
+ return settings.model_copy(deep=True)
diff --git a/packages/core/tests/unit/test_config_cache.py b/packages/core/tests/unit/test_config_cache.py
new file mode 100644
index 00000000..3069ff63
--- /dev/null
+++ b/packages/core/tests/unit/test_config_cache.py
@@ -0,0 +1,74 @@
+"""get_settings() builds Settings once and rebuilds only when its inputs change."""
+
+from pathlib import Path
+
+import pytest
+
+from openexecutive import config
+
+
[email protected](autouse=True)
+def _fresh_cache(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(config, "_settings_cache", None)
+
+
+def _count_builds(monkeypatch: pytest.MonkeyPatch) -> list[int]:
+ builds = [0]
+ real = config.Settings
+
+ def counting(*args: object, **kwargs: object) -> config.Settings:
+ builds[0] += 1
+ return real(*args, **kwargs)
+
+ monkeypatch.setattr(config, "Settings", counting)
+ return builds
+
+
+def test_repeat_calls_reuse_one_build(monkeypatch: pytest.MonkeyPatch) -> None:
+ builds = _count_builds(monkeypatch)
+ first = config.get_settings()
+ second = config.get_settings()
+ assert builds[0] == 1
+ assert first.model_dump() == second.model_dump()
+
+
+def test_an_environment_change_rebuilds(monkeypatch: pytest.MonkeyPatch) -> None:
+ builds = _count_builds(monkeypatch)
+ monkeypatch.setenv("HONCHO_WORKSPACE_ID", "first")
+ assert config.get_settings().honcho_workspace_id == "first"
+ monkeypatch.setenv("HONCHO_WORKSPACE_ID", "second")
+ assert config.get_settings().honcho_workspace_id == "second"
+ monkeypatch.delenv("HONCHO_WORKSPACE_ID")
+ assert config.get_settings().honcho_workspace_id == "openexec"
+ assert builds[0] == 3
+
+
+def test_an_env_file_change_rebuilds(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
+ env_file = tmp_path / ".env"
+ env_file.write_text("HONCHO_WORKSPACE_ID=from-file\n")
+ monkeypatch.delenv("HONCHO_WORKSPACE_ID", raising=False)
+ monkeypatch.setattr(config, "_ENV_FILE", env_file)
+ monkeypatch.setitem(config.Settings.model_config, "env_file", str(env_file))
+ assert config.get_settings().honcho_workspace_id == "from-file"
+ env_file.write_text("HONCHO_WORKSPACE_ID=changed-in-file\n")
+ assert config.get_settings().honcho_workspace_id == "changed-in-file"
+
+
+def test_the_mcp_config_file_appearing_rebuilds(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+) -> None:
+ mcp_file = tmp_path / "mcp_servers.json"
+ monkeypatch.delenv("MCP_ENABLED", raising=False)
+ monkeypatch.setenv("MCP_SERVERS_CONFIG_PATH", str(mcp_file))
+ assert config.get_settings().mcp_enabled is False
+ mcp_file.write_text("{}")
+ assert config.get_settings().mcp_enabled is True
+
+
+def test_a_caller_changing_its_copy_changes_no_one_else() -> None:
+ mine = config.get_settings()
+ mine.honcho_workspace_id = "changed"
+ mine.local_models.append("leaked")
+ theirs = config.get_settings()
+ assert theirs.honcho_workspace_id != "changed"
+ assert "leaked" not in theirs.local_models