FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.
C++ ai-agentsalgorithmic-tradingbloomberg-terminalcppfinancefinancial-marketsfintechgood-first-issueinvestmentinvestment-researchmachine-learningopensourcepythonqtquantitative-financestock-markettrading
refactor(finagent): PersonaRegistry — typed cap, env warning, thread-safety note
tilakpatel2252f96e824eff
diff --git a/fincept-qt/scripts/agents/finagent_core/persona_registry.py b/fincept-qt/scripts/agents/finagent_core/persona_registry.pyindex fb685aaf2..3624234d0 100644--- a/fincept-qt/scripts/agents/finagent_core/persona_registry.py+++ b/fincept-qt/scripts/agents/finagent_core/persona_registry.py@@ -4,6 +4,11 @@ PersonaRegistry — LRU cache of PersonaRuntime instances.Keyed by (user_id, agent_id). Cap default 8, tunable viaFINAGENT_RUNTIME_CACHE_SIZE env var. When full, evicts least-recently-usedruntime and calls its close() before dropping the reference.++Not thread-safe. Caller is expected to run a single event loop per Python+process (today: one QProcess per invocation from the C++ side). If future+code runs async tasks or a thread pool sharing one registry, wrap+get_or_create/clear mutations in a lock."""from __future__ import annotations@@ -11,7 +16,7 @@ from __future__ import annotationsimport loggingimport osfrom collections import OrderedDict-from typing import Any, Dict, Tuple+from typing import Any, Dict, Optional, Tuplefrom finagent_core.persona_runtime import PersonaRuntime@@ -21,13 +26,20 @@ logger = logging.getLogger(__name__)class PersonaRegistry:"""LRU cache of PersonaRuntime objects."""- def __init__(self, cap: int = None):+ def __init__(self, cap: Optional[int] = None):if cap is None:+ raw = os.getenv("FINAGENT_RUNTIME_CACHE_SIZE", "8")try:- cap = int(os.getenv("FINAGENT_RUNTIME_CACHE_SIZE", "8"))+ cap = int(raw)except ValueError:+ logger.warning(+ f"FINAGENT_RUNTIME_CACHE_SIZE={raw!r} not an int; using 8"+ )cap = 8- self.cap = max(1, cap)+ if cap < 1:+ logger.warning(f"PersonaRegistry cap {cap} < 1; clamping to 1")+ cap = 1+ self.cap = capself._items: "OrderedDict[Tuple[str, str], PersonaRuntime]" = OrderedDict()def get_or_create(self,