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): scope agentic memory SQL by (user_id, agent_id)
tilakpatel228fb879223c50
diff --git a/fincept-qt/scripts/agents/finagent_core/modules/memory_module.py b/fincept-qt/scripts/agents/finagent_core/modules/memory_module.pyindex 89b778017..e3f6843b4 100644--- a/fincept-qt/scripts/agents/finagent_core/modules/memory_module.py+++ b/fincept-qt/scripts/agents/finagent_core/modules/memory_module.py@@ -353,6 +353,7 @@ class AgenticMemoryModule:CREATE TABLE IF NOT EXISTS memories (id INTEGER PRIMARY KEY AUTOINCREMENT,user_id TEXT,+ agent_id TEXT NOT NULL DEFAULT 'unnamed',memory_type TEXT,content TEXT,metadata TEXT,@@ -363,11 +364,27 @@ class AgenticMemoryModule:)""")+ # Backward-compat migration: add agent_id column if an older+ # schema (pre-persona-isolation) created the table without it.+ try:+ cursor.execute(+ "ALTER TABLE memories "+ "ADD COLUMN agent_id TEXT NOT NULL DEFAULT 'unnamed'"+ )+ except sqlite3.OperationalError:+ # Column already exists — nothing to do.+ pass+cursor.execute("""CREATE INDEX IF NOT EXISTS idx_user_typeON memories(user_id, memory_type)""")+ cursor.execute("""+ CREATE INDEX IF NOT EXISTS idx_user_agent+ ON memories(user_id, agent_id)+ """)+conn.commit()conn.close()@@ -403,10 +420,11 @@ class AgenticMemoryModule:cursor.execute("""INSERT INTO memories- (user_id, memory_type, content, metadata, relevance_score, created_at, last_accessed)- VALUES (?, ?, ?, ?, ?, ?, ?)+ (user_id, agent_id, memory_type, content, metadata, relevance_score, created_at, last_accessed)+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)""", (self.user_id,+ self.agent_id,memory_type,content,json.dumps(metadata or {}),@@ -417,16 +435,20 @@ class AgenticMemoryModule:memory_id = cursor.lastrowid- # Enforce max memories+ # Enforce max memories (scoped to this persona's rows).cursor.execute("""DELETE FROM memories- WHERE user_id = ? AND id NOT IN (+ WHERE user_id = ? AND agent_id = ? AND id NOT IN (SELECT id FROM memories- WHERE user_id = ?+ WHERE user_id = ? AND agent_id = ?ORDER BY relevance_score DESC, last_accessed DESCLIMIT ?)- """, (self.user_id, self.user_id, self.max_memories))+ """, (+ self.user_id, self.agent_id,+ self.user_id, self.agent_id,+ self.max_memories,+ ))conn.commit()conn.close()@@ -461,8 +483,8 @@ class AgenticMemoryModule:conn = sqlite3.connect(self.db_path)cursor = conn.cursor()- sql = "SELECT * FROM memories WHERE user_id = ?"- params = [self.user_id]+ sql = "SELECT * FROM memories WHERE user_id = ? AND agent_id = ?"+ params = [self.user_id, self.agent_id]if memory_type:sql += " AND memory_type = ?"@@ -514,8 +536,9 @@ class AgenticMemoryModule:cursor = conn.cursor()cursor.execute("""- UPDATE memories SET relevance_score = ? WHERE id = ?- """, (relevance_score, memory_id))+ UPDATE memories SET relevance_score = ?+ WHERE id = ? AND user_id = ? AND agent_id = ?+ """, (relevance_score, memory_id, self.user_id, self.agent_id))conn.commit()conn.close()@@ -531,7 +554,11 @@ class AgenticMemoryModule:conn = sqlite3.connect(self.db_path)cursor = conn.cursor()- cursor.execute("DELETE FROM memories WHERE id = ?", (memory_id,))+ cursor.execute(+ "DELETE FROM memories "+ "WHERE id = ? AND user_id = ? AND agent_id = ?",+ (memory_id, self.user_id, self.agent_id),+ )conn.commit()conn.close()diff --git a/fincept-qt/scripts/agents/finagent_core/tests/test_agentic_memory_isolation.py b/fincept-qt/scripts/agents/finagent_core/tests/test_agentic_memory_isolation.pyindex e81a4a77a..0dba367bc 100644--- a/fincept-qt/scripts/agents/finagent_core/tests/test_agentic_memory_isolation.py+++ b/fincept-qt/scripts/agents/finagent_core/tests/test_agentic_memory_isolation.py@@ -47,3 +47,28 @@ def test_explicit_db_path_overrides_default(monkeypatch, tmp_path):assert mod.db_path == str(custom)mod.store("hello", memory_type="fact")assert custom.exists()+++def test_same_db_file_different_agent_id_is_isolated(tmp_path):+ """Even if two modules share a db_path, agent_id column scopes rows."""+ shared_db = tmp_path / "shared.db"++ buffett = AgenticMemoryModule(+ user_id="alice", agent_id="warren_buffett_agent",+ db_path=str(shared_db),+ )+ ackman = AgenticMemoryModule(+ user_id="alice", agent_id="bill_ackman_agent",+ db_path=str(shared_db),+ )++ buffett.store("Coca-Cola has a moat", memory_type="fact")+ ackman.store("Netflix is overvalued", memory_type="fact")++ buffett_reads = [m["content"] for m in buffett.recall(limit=10)]+ ackman_reads = [m["content"] for m in ackman.recall(limit=10)]++ assert "Coca-Cola has a moat" in buffett_reads+ assert "Coca-Cola has a moat" not in ackman_reads+ assert "Netflix is overvalued" in ackman_reads+ assert "Netflix is overvalued" not in buffett_reads