AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).
refactor(memory): resolve the episodic DB path at call time (#250)
Rufusa33e3279472d
diff --git a/CLAUDE.md b/CLAUDE.mdindex 1f814c90..6a7f0381 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -190,13 +190,14 @@ See `.env.example`. Required: `ANTHROPIC_API_KEY`, `EXEC_EMAIL_ADDRESS` (no defa> "handled overnight" block). Patch it in an autouse fixture, and delete a> stray `packages/core/episodic_memory.db` (gitignored) if one appears.-> **Patching `episodic.DB_PATH` isn't enough:** most readers in-> `memory/episodic.py` (`format_for_prompt`, `get_active_initiatives`, …)-> bind `DB_PATH` as a default argument at import, and the audit logger keeps-> its own path, so both still use `./episodic_memory.db`. If a stray copy of-> that file lacks the `decisions` table, the test fails with "no such table"-> depending on what ran before it. Point them at the test DB too, as the `db`-> fixture in `tests/integration/test_scheduler_runner.py` does.+> **Patching `episodic.DB_PATH` isn't enough:** `memory/episodic.py` reads it+> at call time, but the audit logger keeps its own path, and modules that bind+> `DB_PATH` as a default argument at import (`memory/session_store.py`,+> `knowledge/review_store.py`, …) never see the patch, so they still use+> `./episodic_memory.db`. If a stray copy of that file lacks a table they read,+> the test fails with "no such table" depending on what ran before it. Point+> them at the test DB too, as the `db` fixture in+> `tests/integration/test_scheduler_runner.py` does for the audit logger.> **ContextVar leaks between tests:** a *sync* fixture or test that sets> `current_session` (or any module-level ContextVar) and doesn't reset itdiff --git a/packages/core/openexecutive/memory/episodic.py b/packages/core/openexecutive/memory/episodic.pyindex 1e9b9e10..34c01d5b 100644--- a/packages/core/openexecutive/memory/episodic.py+++ b/packages/core/openexecutive/memory/episodic.py@@ -138,9 +138,19 @@ class OutboundContext(BaseModel):DB_PATH = Path(os.environ.get("EPISODIC_DB_PATH", "./episodic_memory.db"))+def _resolve_db_path(db_path: Path | None) -> Path:+ """Return the caller's path or the current module-level DB_PATH.++ Reading DB_PATH dynamically (not via default-arg binding) lets tests+ monkeypatch `openexecutive.memory.episodic.DB_PATH` and have it actually+ take effect — default arguments capture the value at def time.+ """+ return db_path if db_path is not None else DB_PATH++@contextmanager-def _get_conn(db_path: Path = DB_PATH) -> Generator[sqlite3.Connection, None, None]:- conn = sqlite3.connect(str(db_path))+def _get_conn(db_path: Path | None = None) -> Generator[sqlite3.Connection, None, None]:+ conn = sqlite3.connect(str(_resolve_db_path(db_path)))conn.row_factory = sqlite3.Rowtry:yield conn@@ -149,8 +159,8 @@ def _get_conn(db_path: Path = DB_PATH) -> Generator[sqlite3.Connection, None, Noconn.close()-def initialize_db(db_path: Path = DB_PATH) -> None:- with _get_conn(db_path) as conn:+def initialize_db(db_path: Path | None = None) -> None:+ with _get_conn(_resolve_db_path(db_path)) as conn:conn.executescript("""CREATE TABLE IF NOT EXISTS decisions (id INTEGER PRIMARY KEY AUTOINCREMENT,@@ -561,12 +571,12 @@ def store_decision(department: str = "",session_id: str = "",person_id: int | None = None,- db_path: Path = DB_PATH,+ db_path: Path | None = None,) -> None:now = datetime.now(UTC)dedup_window_start = (now - timedelta(days=7)).isoformat()summary_prefix = summary[:80]- with _get_conn(db_path) as conn:+ with _get_conn(_resolve_db_path(db_path)) as conn:existing = conn.execute("SELECT id, rationale FROM decisions"" WHERE domain = ? AND timestamp > ? AND substr(summary, 1, 80) = ? AND session_id = ?",@@ -608,14 +618,15 @@ def store_initiative(summary: str = "",department: str = "",person_id: int | None = None,- db_path: Path = DB_PATH,+ db_path: Path | None = None,updated_by_person_id: int | None = None,) -> None:+ resolved = _resolve_db_path(db_path)now = datetime.now(UTC).isoformat()is_insert = Falseis_status_change = Falseis_real_update = False- with _get_conn(db_path) as conn:+ with _get_conn(resolved) as conn:existing = conn.execute("SELECT id, status, summary FROM initiatives WHERE title = ?", (title,)).fetchone()@@ -646,7 +657,7 @@ def store_initiative(if existing and is_real_update:# Only a real change answers a check-in — a same-status re-mention# during routine extraction does not.- _resolve_initiative_outreach(int(existing["id"]), updated_by_person_id, db_path)+ _resolve_initiative_outreach(int(existing["id"]), updated_by_person_id, resolved)# Mirror only on a new initiative OR a real status transition.# Idempotent upserts (same title + same status) don't fire — that# would spam the dept peer with redundant notes on every routine@@ -670,12 +681,12 @@ def store_advice(department: str = "",session_id: str = "",person_id: int | None = None,- db_path: Path = DB_PATH,+ db_path: Path | None = None,) -> None:now = datetime.now(UTC)dedup_window_start = (now - timedelta(days=7)).isoformat()advice_prefix = advice_summary[:80]- with _get_conn(db_path) as conn:+ with _get_conn(_resolve_db_path(db_path)) as conn:existing = conn.execute("SELECT id FROM advice_given"" WHERE domain = ? AND timestamp > ? AND substr(advice_summary, 1, 80) = ? AND session_id = ?",@@ -817,10 +828,11 @@ def decisions_awaiting_outcome(return [Decision(**dict(row)) for row in rows]-def get_active_initiatives(db_path: Path = DB_PATH) -> list[Initiative]:- if not db_path.exists():+def get_active_initiatives(db_path: Path | None = None) -> list[Initiative]:+ resolved = _resolve_db_path(db_path)+ if not resolved.exists():return []- with _get_conn(db_path) as conn:+ with _get_conn(resolved) as conn:rows = conn.execute("SELECT * FROM initiatives WHERE status != 'completed' ORDER BY updated_at DESC").fetchall()@@ -833,10 +845,8 @@ def get_recent_initiatives() -> list[Initiative]:"""Most recently kicked-off initiatives, newest first.- Resolves `db_path` lazily (mirrors `get_recent_decisions`/`get_recent_advice`)- so callers and tests pick up a monkeypatched/live `DB_PATH`, unlike- `list_initiatives`'s eager default. Ordered by `created_at` DESC — the- activity rail surfaces these as "kicked off initiative" events.+ Ordered by `created_at` DESC — the activity rail surfaces these as+ "kicked off initiative" events."""resolved = _resolve_db_path(db_path)if not resolved.exists():@@ -848,60 +858,66 @@ def get_recent_initiatives(return [Initiative(**dict(row)) for row in rows]-def list_decisions(db_path: Path = DB_PATH) -> list[Decision]:- if not db_path.exists():+def list_decisions(db_path: Path | None = None) -> list[Decision]:+ resolved = _resolve_db_path(db_path)+ if not resolved.exists():return []- with _get_conn(db_path) as conn:+ with _get_conn(resolved) as conn:rows = conn.execute("SELECT * FROM decisions ORDER BY timestamp DESC").fetchall()return [Decision(**dict(row)) for row in rows]-def list_initiatives(db_path: Path = DB_PATH) -> list[Initiative]:- if not db_path.exists():+def list_initiatives(db_path: Path | None = None) -> list[Initiative]:+ resolved = _resolve_db_path(db_path)+ if not resolved.exists():return []- with _get_conn(db_path) as conn:+ with _get_conn(resolved) as conn:rows = conn.execute("SELECT * FROM initiatives ORDER BY updated_at DESC").fetchall()return [Initiative(**dict(row)) for row in rows]-def list_advice(db_path: Path = DB_PATH) -> list[Advice]:- if not db_path.exists():+def list_advice(db_path: Path | None = None) -> list[Advice]:+ resolved = _resolve_db_path(db_path)+ if not resolved.exists():return []- with _get_conn(db_path) as conn:+ with _get_conn(resolved) as conn:rows = conn.execute("SELECT * FROM advice_given ORDER BY timestamp DESC").fetchall()return [Advice(**dict(row)) for row in rows]-def get_decision(decision_id: int, db_path: Path = DB_PATH) -> Decision | None:- if not db_path.exists():+def get_decision(decision_id: int, db_path: Path | None = None) -> Decision | None:+ resolved = _resolve_db_path(db_path)+ if not resolved.exists():return None- with _get_conn(db_path) as conn:+ with _get_conn(resolved) as conn:row = conn.execute("SELECT * FROM decisions WHERE id = ?", (decision_id,)).fetchone()return Decision(**dict(row)) if row else None-def get_initiative(initiative_id: int, db_path: Path = DB_PATH) -> Initiative | None:- if not db_path.exists():+def get_initiative(initiative_id: int, db_path: Path | None = None) -> Initiative | None:+ resolved = _resolve_db_path(db_path)+ if not resolved.exists():return None- with _get_conn(db_path) as conn:+ with _get_conn(resolved) as conn:row = conn.execute("SELECT * FROM initiatives WHERE id = ?", (initiative_id,)).fetchone()return Initiative(**dict(row)) if row else None-def get_advice(advice_id: int, db_path: Path = DB_PATH) -> Advice | None:- if not db_path.exists():+def get_advice(advice_id: int, db_path: Path | None = None) -> Advice | None:+ resolved = _resolve_db_path(db_path)+ if not resolved.exists():return None- with _get_conn(db_path) as conn:+ with _get_conn(resolved) as conn:row = conn.execute("SELECT * FROM advice_given WHERE id = ?", (advice_id,)).fetchone()@@ -916,8 +932,9 @@ def update_decision(rationale: str | None = None,outcome: str | None = None,tags: str | None = None,- db_path: Path = DB_PATH,+ db_path: Path | None = None,) -> bool:+ resolved = _resolve_db_path(db_path)fields: list[tuple[str, str]] = []if domain is not None:fields.append(("domain", domain))@@ -930,10 +947,10 @@ def update_decision(if tags is not None:fields.append(("tags", tags))if not fields:- return get_decision(decision_id, db_path) is not None+ return get_decision(decision_id, resolved) is not Noneset_clause = ", ".join(f"{name} = ?" for name, _ in fields)values = [value for _, value in fields] + [decision_id]- with _get_conn(db_path) as conn:+ with _get_conn(resolved) as conn:cursor = conn.execute(f"UPDATE decisions SET {set_clause} WHERE id = ?", values)@@ -946,9 +963,10 @@ def update_initiative(title: str | None = None,status: str | None = None,summary: str | None = None,- db_path: Path = DB_PATH,+ db_path: Path | None = None,updated_by_person_id: int | None = None,) -> bool:+ resolved = _resolve_db_path(db_path)fields: list[tuple[str, str]] = []if title is not None:fields.append(("title", title))@@ -957,17 +975,17 @@ def update_initiative(if summary is not None:fields.append(("summary", summary))if not fields:- return get_initiative(initiative_id, db_path) is not None+ return get_initiative(initiative_id, resolved) is not Nonefields.append(("updated_at", datetime.now(UTC).isoformat()))set_clause = ", ".join(f"{name} = ?" for name, _ in fields)values = [value for _, value in fields] + [initiative_id]- with _get_conn(db_path) as conn:+ with _get_conn(resolved) as conn:cursor = conn.execute(f"UPDATE initiatives SET {set_clause} WHERE id = ?", values)updated = cursor.rowcount > 0if updated:- _resolve_initiative_outreach(initiative_id, updated_by_person_id, db_path)+ _resolve_initiative_outreach(initiative_id, updated_by_person_id, resolved)return updated@@ -1007,8 +1025,9 @@ def update_advice(domain: str | None = None,query_summary: str | None = None,advice_summary: str | None = None,- db_path: Path = DB_PATH,+ db_path: Path | None = None,) -> bool:+ resolved = _resolve_db_path(db_path)fields: list[tuple[str, str]] = []if domain is not None:fields.append(("domain", domain))@@ -1017,36 +1036,39 @@ def update_advice(if advice_summary is not None:fields.append(("advice_summary", advice_summary))if not fields:- return get_advice(advice_id, db_path) is not None+ return get_advice(advice_id, resolved) is not Noneset_clause = ", ".join(f"{name} = ?" for name, _ in fields)values = [value for _, value in fields] + [advice_id]- with _get_conn(db_path) as conn:+ with _get_conn(resolved) as conn:cursor = conn.execute(f"UPDATE advice_given SET {set_clause} WHERE id = ?", values)return cursor.rowcount > 0-def delete_decision(decision_id: int, db_path: Path = DB_PATH) -> bool:- if not db_path.exists():+def delete_decision(decision_id: int, db_path: Path | None = None) -> bool:+ resolved = _resolve_db_path(db_path)+ if not resolved.exists():return False- with _get_conn(db_path) as conn:+ with _get_conn(resolved) as conn:cursor = conn.execute("DELETE FROM decisions WHERE id = ?", (decision_id,))return cursor.rowcount > 0-def delete_initiative(initiative_id: int, db_path: Path = DB_PATH) -> bool:- if not db_path.exists():+def delete_initiative(initiative_id: int, db_path: Path | None = None) -> bool:+ resolved = _resolve_db_path(db_path)+ if not resolved.exists():return False- with _get_conn(db_path) as conn:+ with _get_conn(resolved) as conn:cursor = conn.execute("DELETE FROM initiatives WHERE id = ?", (initiative_id,))return cursor.rowcount > 0-def delete_advice(advice_id: int, db_path: Path = DB_PATH) -> bool:- if not db_path.exists():+def delete_advice(advice_id: int, db_path: Path | None = None) -> bool:+ resolved = _resolve_db_path(db_path)+ if not resolved.exists():return False- with _get_conn(db_path) as conn:+ with _get_conn(resolved) as conn:cursor = conn.execute("DELETE FROM advice_given WHERE id = ?", (advice_id,))return cursor.rowcount > 0@@ -1070,16 +1092,6 @@ _VALID_OUTBOUND_CHANNELS = {"discord_dm", "telegram", "slack_dm", "email"}_MAX_OUTBOUND_CONTEXT_CHARS = 2000-def _resolve_db_path(db_path: Path | None) -> Path:- """Return the caller's path or the current module-level DB_PATH.-- Reading DB_PATH dynamically (not via default-arg binding) lets tests- monkeypatch `openexecutive.memory.episodic.DB_PATH` and have it actually- take effect — default arguments capture the value at def time.- """- return db_path if db_path is not None else DB_PATH--_VALID_INSERT_STATUSES = {"pending", "done"}@@ -1816,7 +1828,7 @@ def last_contact_at_by_person(def format_for_prompt(- db_path: Path = DB_PATH,+ db_path: Path | None = None,max_chars: int = 2500,session_id: str = "",) -> str:@@ -1830,9 +1842,10 @@ def format_for_prompt(Output is bounded by `max_chars`. When over budget, oldest advice is droppedfirst, then oldest decisions. Initiatives are always kept."""- decisions = get_recent_decisions(limit=5, db_path=db_path, session_id=session_id)- initiatives = get_active_initiatives(db_path=db_path)- advice_items = get_recent_advice(limit=2, db_path=db_path, session_id=session_id)+ resolved = _resolve_db_path(db_path)+ decisions = get_recent_decisions(limit=5, db_path=resolved, session_id=session_id)+ initiatives = get_active_initiatives(db_path=resolved)+ advice_items = get_recent_advice(limit=2, db_path=resolved, session_id=session_id)if not decisions and not initiatives and not advice_items:return ""@@ -2681,7 +2694,7 @@ def _audit_extraction(async def extract_and_store(user_message: str,assistant_response: str,- db_path: Path = DB_PATH,+ db_path: Path | None = None,session_id: str = "",audit_session_id: str | None = None,audit_turn_id: str | None = None,@@ -2700,6 +2713,8 @@ async def extract_and_store("""from openexecutive.audit.context import get_active_ids, set_turn+ resolved = _resolve_db_path(db_path)+# Fall back per field, not as a pair. Binding a half-empty snapshot# would erase the ambient counterpart — a row under a session with no# turn, or a turn that joins to nothing — which is worse than either@@ -2712,13 +2727,13 @@ async def extract_and_store(if (effective_session, effective_turn) == (ambient_session, ambient_turn):await _extract_and_store(- user_message, assistant_response, db_path, session_id+ user_message, assistant_response, resolved, session_id)returnwith set_turn(session_id=effective_session, turn_id=effective_turn):await _extract_and_store(- user_message, assistant_response, db_path, session_id+ user_message, assistant_response, resolved, session_id)diff --git a/packages/core/tests/integration/test_scheduler_runner.py b/packages/core/tests/integration/test_scheduler_runner.pyindex 71774bd8..95d1ad6c 100644--- a/packages/core/tests/integration/test_scheduler_runner.py+++ b/packages/core/tests/integration/test_scheduler_runner.py@@ -8,13 +8,11 @@ from __future__ import annotationsimport asynciofrom datetime import UTC, datetime, timedelta-from functools import partialfrom pathlib import Pathimport pytestfrom openexecutive.audit import logger as audit_logger-from openexecutive.memory import episodicfrom openexecutive.memory.episodic import (ScheduledAction,claim_due_actions,@@ -30,15 +28,9 @@ def db(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:db_path = tmp_path / "episodic.db"initialize_db(db_path)monkeypatch.setattr("openexecutive.memory.episodic.DB_PATH", db_path)- # Patching DB_PATH misses two things _execute_action touches:- # format_for_prompt() bound DB_PATH as a default argument at import, and- # the audit logger keeps its own path. Both fell back to- # ./episodic_memory.db in the working directory, so the retry test failed- # with "no such table: decisions" whenever that file held only the audit- # rows an earlier test had written there.- monkeypatch.setattr(- episodic, "format_for_prompt", partial(episodic.format_for_prompt, db_path=db_path)- )+ # Patching DB_PATH doesn't reach the audit logger, which keeps its own+ # path, so _execute_action's audit rows would otherwise land in+ # ./episodic_memory.db in the working directory (see CLAUDE.md, Testing).monkeypatch.setattr(audit_logger, "_default_logger", audit_logger.AuditLogger(db_path=db_path))return db_pathdiff --git a/packages/core/tests/unit/test_episodic_db_path.py b/packages/core/tests/unit/test_episodic_db_path.pynew file mode 100644index 00000000..de82337e--- /dev/null+++ b/packages/core/tests/unit/test_episodic_db_path.py@@ -0,0 +1,47 @@+"""`memory/episodic.py` looks `DB_PATH` up when a function runs, not at import.++A `db_path: Path = DB_PATH` default is evaluated once, at import, so a test+that patches `episodic.DB_PATH` never reached a function declared that way: it+kept reading `./episodic_memory.db` in the working directory, and failed with+"no such table" whenever a partial copy of that file was lying around.+"""++from __future__ import annotations++import inspect+from pathlib import Path++import pytest++from openexecutive.memory import episodic+++def test_format_for_prompt_reads_the_patched_db_path(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ db_path = tmp_path / "episodic.db"+ episodic.initialize_db(db_path)+ episodic.store_decision("strategy", "Open a second warehouse in Leeds", db_path=db_path)+ monkeypatch.setattr(episodic, "DB_PATH", db_path)++ assert "Open a second warehouse in Leeds" in episodic.format_for_prompt()+++def test_no_db_path_default_is_bound_at_import() -> None:+ """Guards the pattern rather than one function, so a new+ `db_path: Path = DB_PATH` cannot bring the trap back."""+ defaults = {+ name: inspect.signature(fn).parameters["db_path"].default+ for name, fn in inspect.getmembers(episodic, inspect.isfunction)+ if fn.__module__ == episodic.__name__+ and "db_path" in inspect.signature(fn).parameters+ }+ # Not vacuous: `_get_conn` is wrapped by @contextmanager, and signature()+ # follows its __wrapped__ to the real parameters.+ assert {"_get_conn", "initialize_db", "format_for_prompt"} <= defaults.keys()+ bound = sorted(+ name+ for name, default in defaults.items()+ if default is not None and default is not inspect.Parameter.empty+ )+ assert bound == []diff --git a/packages/core/tests/unit/test_fixture_loader_honcho_reset.py b/packages/core/tests/unit/test_fixture_loader_honcho_reset.pyindex 3bf53497..11c750e1 100644--- a/packages/core/tests/unit/test_fixture_loader_honcho_reset.py+++ b/packages/core/tests/unit/test_fixture_loader_honcho_reset.py@@ -43,14 +43,25 @@ def _isolate_dbs(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:Also initialize each DB's schema, otherwise ``reset_all_state``'s seedstep hits ``no such table: departments`` against the empty tmp files."""+ from openexecutive.alerts import store as alerts_store+ from openexecutive.audit.logger import AuditLoggerfrom openexecutive.departments import store as dept_store+ from openexecutive.evals.persistence import initialize_eval_runs_dbfrom openexecutive.memory import episodicfrom openexecutive.people import store as people_store+ from openexecutive.workflows.persistence import initialize_runs_db- monkeypatch.setattr(episodic, "DB_PATH", tmp_path / "episodic.db")+ episodic_path = tmp_path / "episodic.db"+ monkeypatch.setattr(episodic, "DB_PATH", episodic_path)monkeypatch.setattr(people_store, "DB_PATH", tmp_path / "people.db")monkeypatch.setattr(dept_store, "DB_PATH", tmp_path / "depts.db")episodic.initialize_db()+ # The reset also wipes the alert, workflow-run, eval-run and audit tables+ # that share the episodic DB, and fails on one that doesn't exist.+ alerts_store.initialize_db(episodic_path)+ initialize_runs_db(episodic_path)+ initialize_eval_runs_db(episodic_path)+ AuditLogger(db_path=episodic_path) # the constructor creates audit_logpeople_store.initialize_db()dept_store.initialize_db()diff --git a/packages/core/tests/unit/test_fixture_loader_honcho_workspace_isolation.py b/packages/core/tests/unit/test_fixture_loader_honcho_workspace_isolation.pyindex 974800d9..deb4f072 100644--- a/packages/core/tests/unit/test_fixture_loader_honcho_workspace_isolation.py+++ b/packages/core/tests/unit/test_fixture_loader_honcho_workspace_isolation.py@@ -40,14 +40,25 @@ def settings_stub(tmp_path: Path) -> Any:@pytest.fixture(autouse=True)def _isolate_dbs(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:+ from openexecutive.alerts import store as alerts_store+ from openexecutive.audit.logger import AuditLoggerfrom openexecutive.departments import store as dept_store+ from openexecutive.evals.persistence import initialize_eval_runs_dbfrom openexecutive.memory import episodicfrom openexecutive.people import store as people_store+ from openexecutive.workflows.persistence import initialize_runs_db- monkeypatch.setattr(episodic, "DB_PATH", tmp_path / "episodic.db")+ episodic_path = tmp_path / "episodic.db"+ monkeypatch.setattr(episodic, "DB_PATH", episodic_path)monkeypatch.setattr(people_store, "DB_PATH", tmp_path / "people.db")monkeypatch.setattr(dept_store, "DB_PATH", tmp_path / "depts.db")episodic.initialize_db()+ # The reset also wipes the alert, workflow-run, eval-run and audit tables+ # that share the episodic DB, and fails on one that doesn't exist.+ alerts_store.initialize_db(episodic_path)+ initialize_runs_db(episodic_path)+ initialize_eval_runs_db(episodic_path)+ AuditLogger(db_path=episodic_path) # the constructor creates audit_logpeople_store.initialize_db()dept_store.initialize_db()# Point the Honcho state file at the test's company profile dir.