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.

fix(finagent): resources — honest None typing, tilde expansion, docstrings

tilakpatel2246160f390595
diff --git a/fincept-qt/scripts/agents/finagent_core/resources.py b/fincept-qt/scripts/agents/finagent_core/resources.py
index 3ceab0393..69435bc5c 100644
--- a/fincept-qt/scripts/agents/finagent_core/resources.py
+++ b/fincept-qt/scripts/agents/finagent_core/resources.py
@@ -15,8 +15,11 @@ from typing import Optional
_UNSAFE_RE = re.compile(r"[^A-Za-z0-9_\-]")
-def sanitize(raw: str) -> str:
- """Sanitise an id for safe filesystem use."""
+def sanitize(raw: Optional[str]) -> str:
+ """Sanitise an id for safe filesystem use.
+
+ Raises ValueError on None or empty/whitespace-only input.
+ """
if raw is None or not str(raw).strip():
raise ValueError("id must be non-empty")
return _UNSAFE_RE.sub("_", str(raw).strip())
@@ -33,7 +36,7 @@ def base_dir() -> str:
"""
env = os.getenv("FINAGENT_DATA_DIR")
if env:
- return env
+ return str(Path(env).expanduser())
if os.name == "nt":
root = os.getenv("LOCALAPPDATA") or str(Path.home() / "AppData" / "Local")
return str(Path(root) / "com.fincept.terminal" / "finagent")
@@ -42,28 +45,34 @@ def base_dir() -> str:
def persona_dir(user_id: str, agent_id: str) -> str:
+ """Return the directory for one persona's state. Does not create it."""
u = sanitize(user_id)
a = sanitize(agent_id)
return str(Path(base_dir()) / "users" / u / "personas" / a)
def ensure_persona_dir(user_id: str, agent_id: str) -> str:
+ """Create the persona directory tree (if missing) and return the path."""
d = persona_dir(user_id, agent_id)
Path(d).mkdir(parents=True, exist_ok=True)
return d
def sessions_db(user_id: str, agent_id: str) -> str:
+ """Path to the SQLite file storing Agno session history for this persona."""
return str(Path(persona_dir(user_id, agent_id)) / "sessions.db")
def memory_db(user_id: str, agent_id: str) -> str:
+ """Path to the SQLite file for Agno long-term memory for this persona."""
return str(Path(persona_dir(user_id, agent_id)) / "memory.db")
def knowledge_dir(user_id: str, agent_id: str) -> str:
+ """Directory for this persona's knowledge-base vector store."""
return str(Path(persona_dir(user_id, agent_id)) / "knowledge")
def agentic_memory_db(user_id: str, agent_id: str) -> str:
+ """Path to the SQLite file for AgenticMemoryModule storage for this persona."""
return str(Path(persona_dir(user_id, agent_id)) / "agentic_memory.db")
diff --git a/fincept-qt/scripts/agents/finagent_core/tests/test_resources.py b/fincept-qt/scripts/agents/finagent_core/tests/test_resources.py
index 5b9681f36..9cc4ca0a5 100644
--- a/fincept-qt/scripts/agents/finagent_core/tests/test_resources.py
+++ b/fincept-qt/scripts/agents/finagent_core/tests/test_resources.py
@@ -17,11 +17,13 @@ def test_sanitize_replaces_unsafe_chars():
assert resources.sanitize("spaces here") == "spaces_here"
-def test_sanitize_rejects_empty():
+def test_sanitize_rejects_empty_or_none():
with pytest.raises(ValueError):
resources.sanitize("")
with pytest.raises(ValueError):
resources.sanitize(" ")
+ with pytest.raises(ValueError):
+ resources.sanitize(None)
def test_base_dir_respects_env_var(monkeypatch, tmp_path):
@@ -31,6 +33,13 @@ def test_base_dir_respects_env_var(monkeypatch, tmp_path):
assert Path(base) == tmp_path
+def test_base_dir_expands_tilde(monkeypatch):
+ monkeypatch.setenv("FINAGENT_DATA_DIR", "~/finagent-test-xyz")
+ base = resources.base_dir()
+ assert "~" not in base
+ assert base.endswith("finagent-test-xyz")
+
+
def test_persona_dir_structure(monkeypatch, tmp_path):
monkeypatch.setenv("FINAGENT_DATA_DIR", str(tmp_path))
d = resources.persona_dir("alice", "warren_buffett_agent")