Skip to content

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.

refactor(finagent): address Task 3 code review — wire storage, strengthen close test, log persona context

tilakpatel221140a5d321e3
diff --git a/fincept-qt/scripts/agents/finagent_core/persona_runtime.py b/fincept-qt/scripts/agents/finagent_core/persona_runtime.py
index 99f1ebb4c..0df8e32ff 100644
--- a/fincept-qt/scripts/agents/finagent_core/persona_runtime.py
+++ b/fincept-qt/scripts/agents/finagent_core/persona_runtime.py
@@ -11,7 +11,7 @@ PersonaRegistry creates these, caches them, and closes them on eviction.
from __future__ import annotations
import logging
-from typing import Any, Dict, List, Optional
+from typing import Any, Dict, Optional
from finagent_core import resources
@@ -89,7 +89,16 @@ def build_agno_agent(config: Dict[str, Any],
tmp._create_storage = lambda _cfg: storage_backend
if knowledge_agent_kwargs is not None:
tmp._setup_knowledge = lambda _cfg: knowledge_agent_kwargs
- return tmp._create_agent(config)
+
+ # Ensure CoreAgent actually wires the storage/knowledge we built. Its
+ # _create_storage/_setup_knowledge gates on config keys — inject empty
+ # dicts so the gates open and our monkey-patches are used.
+ cfg = dict(config)
+ if storage_backend is not None and "storage" not in cfg and not cfg.get("session_id"):
+ cfg["storage"] = {}
+ if knowledge_agent_kwargs is not None and "knowledge" not in cfg:
+ cfg["knowledge"] = {}
+ return tmp._create_agent(cfg)
class PersonaRuntime:
@@ -141,6 +150,11 @@ class PersonaRuntime:
create_user_memories=mc.get("create_user_memories", True),
create_session_summary=mc.get("create_session_summary", True),
)
+ if memory_backend is None:
+ logger.error(
+ f"Memory backend build returned None for persona "
+ f"user_id={user_id!r} agent_id={agent_id!r}"
+ )
storage_cfg = config.get("storage", {})
storage_backend = None
@@ -150,6 +164,11 @@ class PersonaRuntime:
db_path=sc.get("db_path") or resources.sessions_db(user_id, agent_id),
table_name=sc.get("table_name", "agent_sessions"),
)
+ if storage_backend is None:
+ logger.error(
+ f"Storage backend build returned None for persona "
+ f"user_id={user_id!r} agent_id={agent_id!r}"
+ )
knowledge_module = None
knowledge_agent_kwargs = None
@@ -158,6 +177,11 @@ class PersonaRuntime:
knowledge_module = _build_knowledge_module(knowledge_cfg, user_id, agent_id, api_keys)
if knowledge_module is not None:
knowledge_agent_kwargs = knowledge_module.to_agent_config()
+ if knowledge_module is None:
+ logger.error(
+ f"Knowledge module build returned None for persona "
+ f"user_id={user_id!r} agent_id={agent_id!r}"
+ )
agno_agent = build_agno_agent(
config=config,
diff --git a/fincept-qt/scripts/agents/finagent_core/tests/test_persona_runtime.py b/fincept-qt/scripts/agents/finagent_core/tests/test_persona_runtime.py
index 9d48b1494..9676a2b81 100644
--- a/fincept-qt/scripts/agents/finagent_core/tests/test_persona_runtime.py
+++ b/fincept-qt/scripts/agents/finagent_core/tests/test_persona_runtime.py
@@ -65,8 +65,13 @@ def test_close_is_idempotent(monkeypatch, tmp_path):
mock_build.return_value = MagicMock()
rt = PersonaRuntime.build("u", "a", _minimal_config(), api_keys={})
+ fake_backend = MagicMock()
+ rt.memory_backend = fake_backend
+ rt._closed = False # force re-enable for this test after stubbed build
+
rt.close()
- rt.close() # must not raise
+ rt.close() # must not raise, must not double-close
+ assert fake_backend.close.call_count == 1
def test_two_personas_same_user_use_different_paths(monkeypatch, tmp_path):