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
feat(finagent): add resources module for per-persona path derivation
tilakpatel22362b9552a3c4
diff --git a/fincept-qt/scripts/agents/finagent_core/resources.py b/fincept-qt/scripts/agents/finagent_core/resources.pynew file mode 100644index 000000000..3ceab0393--- /dev/null+++ b/fincept-qt/scripts/agents/finagent_core/resources.py@@ -0,0 +1,69 @@+"""+Path derivation for per-persona runtime state.++Every stateful resource (sessions, memory, knowledge, agentic memory) lives+under a path keyed by (user_id, agent_id). This module is the single source+of truth for those paths — no other module should compose them by hand.+"""++import os+import re+from pathlib import Path+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."""+ if raw is None or not str(raw).strip():+ raise ValueError("id must be non-empty")+ return _UNSAFE_RE.sub("_", str(raw).strip())+++def base_dir() -> str:+ """+ Root directory for all finagent persona state.++ Overridable via FINAGENT_DATA_DIR env var. Otherwise defaults to+ <LOCALAPPDATA>/com.fincept.terminal/finagent on Windows, or+ $XDG_DATA_HOME/com.fincept.terminal/finagent (~/.local/share fallback)+ elsewhere.+ """+ env = os.getenv("FINAGENT_DATA_DIR")+ if env:+ return env+ if os.name == "nt":+ root = os.getenv("LOCALAPPDATA") or str(Path.home() / "AppData" / "Local")+ return str(Path(root) / "com.fincept.terminal" / "finagent")+ xdg = os.getenv("XDG_DATA_HOME") or str(Path.home() / ".local" / "share")+ return str(Path(xdg) / "com.fincept.terminal" / "finagent")+++def persona_dir(user_id: str, agent_id: str) -> str:+ 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:+ 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:+ return str(Path(persona_dir(user_id, agent_id)) / "sessions.db")+++def memory_db(user_id: str, agent_id: str) -> str:+ return str(Path(persona_dir(user_id, agent_id)) / "memory.db")+++def knowledge_dir(user_id: str, agent_id: str) -> str:+ return str(Path(persona_dir(user_id, agent_id)) / "knowledge")+++def agentic_memory_db(user_id: str, agent_id: str) -> str:+ return str(Path(persona_dir(user_id, agent_id)) / "agentic_memory.db")diff --git a/fincept-qt/scripts/agents/finagent_core/tests/__init__.py b/fincept-qt/scripts/agents/finagent_core/tests/__init__.pynew file mode 100644index 000000000..e69de29bbdiff --git a/fincept-qt/scripts/agents/finagent_core/tests/test_resources.py b/fincept-qt/scripts/agents/finagent_core/tests/test_resources.pynew file mode 100644index 000000000..5b9681f36--- /dev/null+++ b/fincept-qt/scripts/agents/finagent_core/tests/test_resources.py@@ -0,0 +1,61 @@+import os+from pathlib import Path++import pytest++from finagent_core import resources+++def test_sanitize_allows_alphanumerics_underscore_dash():+ assert resources.sanitize("warren_buffett_agent") == "warren_buffett_agent"+ assert resources.sanitize("user-42") == "user-42"+++def test_sanitize_replaces_unsafe_chars():+ assert resources.sanitize("user@host") == "user_host"+ assert resources.sanitize("a/b\\c") == "a_b_c"+ assert resources.sanitize("spaces here") == "spaces_here"+++def test_sanitize_rejects_empty():+ with pytest.raises(ValueError):+ resources.sanitize("")+ with pytest.raises(ValueError):+ resources.sanitize(" ")+++def test_base_dir_respects_env_var(monkeypatch, tmp_path):+ monkeypatch.setenv("FINAGENT_DATA_DIR", str(tmp_path))+ # Force re-read+ base = resources.base_dir()+ assert Path(base) == tmp_path+++def test_persona_dir_structure(monkeypatch, tmp_path):+ monkeypatch.setenv("FINAGENT_DATA_DIR", str(tmp_path))+ d = resources.persona_dir("alice", "warren_buffett_agent")+ expected = tmp_path / "users" / "alice" / "personas" / "warren_buffett_agent"+ assert Path(d) == expected+++def test_all_per_persona_files(monkeypatch, tmp_path):+ monkeypatch.setenv("FINAGENT_DATA_DIR", str(tmp_path))+ u, a = "alice", "warren_buffett_agent"+ base = tmp_path / "users" / "alice" / "personas" / "warren_buffett_agent"+ assert Path(resources.sessions_db(u, a)) == base / "sessions.db"+ assert Path(resources.memory_db(u, a)) == base / "memory.db"+ assert Path(resources.knowledge_dir(u, a)) == base / "knowledge"+ assert Path(resources.agentic_memory_db(u, a)) == base / "agentic_memory.db"+++def test_ensure_persona_dir_creates_tree(monkeypatch, tmp_path):+ monkeypatch.setenv("FINAGENT_DATA_DIR", str(tmp_path))+ d = resources.ensure_persona_dir("alice", "warren_buffett_agent")+ assert Path(d).is_dir()+++def test_two_personas_have_disjoint_paths(monkeypatch, tmp_path):+ monkeypatch.setenv("FINAGENT_DATA_DIR", str(tmp_path))+ a1 = resources.memory_db("alice", "warren_buffett_agent")+ a2 = resources.memory_db("alice", "bill_ackman_agent")+ assert a1 != a2