DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(memory): preserve account scope and reject redirected workspace stores
Bingxi Zhao (Frank)6ca208b2f04c
diff --git a/deeptutor/services/memory/snapshot/adapters.py b/deeptutor/services/memory/snapshot/adapters.pyindex 6518168ce..a87d7f1c9 100644--- a/deeptutor/services/memory/snapshot/adapters.py+++ b/deeptutor/services/memory/snapshot/adapters.py@@ -21,9 +21,12 @@ from pathlib import Pathimport sqlite3from typing import Any+from deeptutor.multi_user.paths import get_account_path_servicefrom deeptutor.services.memory.paths import Surfacefrom deeptutor.services.memory.snapshot.entity import Entity, EntityStamp-from deeptutor.services.path_service import PathService, get_path_service+from deeptutor.services.path_service import get_path_service+from deeptutor.services.workspace.context import WorkspacePathService, WorkspaceScope+from deeptutor.services.workspace.models import WorkspaceErrorlogger = logging.getLogger(__name__)@@ -76,7 +79,8 @@ def _iter_chat_dbs() -> list[tuple[str, Path]]:redirected store is skipped rather than raised, so one broken bindingcannot blank an account-wide scan."""- pairs: list[tuple[str, Path]] = [("", get_path_service().get_chat_history_db())]+ account = get_account_path_service()+ pairs: list[tuple[str, Path]] = [("", account.get_chat_history_db())]try:from deeptutor.services.workspace import get_content_workspace_service@@ -87,20 +91,18 @@ def _iter_chat_dbs() -> list[tuple[str, Path]]:)return pairsfor binding in bindings:- private_root = (binding.root / ".deeptutor").resolve()- data_root = private_root / "data"- if private_root.is_symlink() or data_root.is_symlink():+ try:+ paths = WorkspacePathService(+ account,+ WorkspaceScope(binding.workspace_id, account.workspace_root, binding.root),+ )+ except WorkspaceError:logger.warning("workspace %s skipped from memory aggregation: redirected data store",binding.workspace_id,)continue- pairs.append(- (- f"ws:{binding.workspace_id}:",- PathService(workspace_root=data_root).get_chat_history_db(),- )- )+ pairs.append((f"ws:{binding.workspace_id}:", paths.get_chat_history_db()))return pairsdiff --git a/tests/services/memory/test_snapshot_probes.py b/tests/services/memory/test_snapshot_probes.pyindex c88ee6542..603ce9c65 100644--- a/tests/services/memory/test_snapshot_probes.py+++ b/tests/services/memory/test_snapshot_probes.py@@ -52,7 +52,7 @@ def _make_db(path: Path) -> sqlite3.Connection:def chat_db(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> sqlite3.Connection:db_path = tmp_path / "chat_history.db"conn = _make_db(db_path)- monkeypatch.setattr(adapters, "get_path_service", lambda: _FakePathService(db_path))+ monkeypatch.setattr(adapters, "get_account_path_service", lambda: _FakePathService(db_path))yield connconn.close()@@ -135,7 +135,7 @@ def test_probe_returns_empty_without_a_database(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:missing = tmp_path / "nope.db"- monkeypatch.setattr(adapters, "get_path_service", lambda: _FakePathService(missing))+ monkeypatch.setattr(adapters, "get_account_path_service", lambda: _FakePathService(missing))assert adapters.probe_chat_entities() == []diff --git a/tests/services/memory/test_snapshot_workspace_aggregation.py b/tests/services/memory/test_snapshot_workspace_aggregation.pyindex dedc67880..cd76c55d8 100644--- a/tests/services/memory/test_snapshot_workspace_aggregation.py+++ b/tests/services/memory/test_snapshot_workspace_aggregation.py@@ -28,6 +28,7 @@ from deeptutor.services.path_service import PathServiceclass _FakePathService:def __init__(self, db_path: Path) -> None:self._db_path = db_path+ self.workspace_root = db_path.parentdef get_chat_history_db(self) -> Path:return self._db_path@@ -110,7 +111,7 @@ def _with_bindings(default_db: Path,bindings: list[SimpleNamespace],) -> None:- monkeypatch.setattr(adapters, "get_path_service", lambda: _FakePathService(default_db))+ monkeypatch.setattr(adapters, "get_account_path_service", lambda: _FakePathService(default_db))monkeypatch.setattr("deeptutor.services.workspace.get_content_workspace_service",lambda: SimpleNamespace(registered_bindings=lambda: bindings),@@ -219,9 +220,7 @@ def test_broken_binding_is_skipped_fail_open() -> None:_add_session(default_db, "s1", "Still visible")# A binding whose data store does not exist (moved / never provisioned)- # contributes nothing but must not blank the account-wide scan. (The- # symlink-refusal branch mirrors WorkspacePathService and cannot be- # exercised here — creating symlinks on Windows needs privileges.)+ # contributes nothing but must not blank the account-wide scan._with_bindings(monkeypatch,tmp_path / "default" / "chat_history.db",@@ -251,3 +250,69 @@ def test_enumeration_failure_falls_back_to_default_only(entities = adapters.read_chat_entities()assert [e.id for e in entities] == ["s1"]+++ "redirected",+ [".deeptutor", ".deeptutor/data", ".deeptutor/data/user", ".deeptutor/data/user/workspace"],+)+def test_redirected_private_store_is_skipped(+ tmp_path: Path, default_db: sqlite3.Connection, monkeypatch: pytest.MonkeyPatch, redirected: str+) -> None:+ _add_session(default_db, "s-default", "Default chat")+ root = tmp_path / "workspace"+ target = tmp_path / "external"+ target.mkdir()+ link = root / redirected+ link.parent.mkdir(parents=True)+ try:+ link.symlink_to(target, target_is_directory=True)+ except OSError as exc:+ pytest.skip(f"Directory symlinks unavailable: {exc}")+ db_path = PathService(workspace_root=_workspace_data_root(root)).get_chat_history_db()+ db_path.parent.mkdir(parents=True, exist_ok=True)+ with _make_db(db_path) as conn:+ _add_session(conn, "private", "Must not read redirected storage", question="Private quiz")+ _with_bindings(+ monkeypatch, tmp_path / "default" / "chat_history.db", [_binding("linked", root)]+ )++ assert [e.id for e in adapters.read_chat_entities()] == ["s-default"]+ assert [e.id for e in adapters.probe_chat_entities()] == ["s-default"]+ assert adapters.read_quiz_entities() == []+++def test_active_custom_workspace_does_not_replace_default(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ from deeptutor.services.workspace.context import (+ WorkspacePathService,+ WorkspaceScope,+ workspace_context,+ )++ account = PathService(workspace_root=tmp_path / "account")+ root = tmp_path / "custom"+ scope = WorkspaceScope("custom", account.workspace_root, root)+ custom = WorkspacePathService(account, scope)+ for paths, title in [(account, "Default"), (custom, "Custom")]:+ db_path = paths.get_chat_history_db()+ db_path.parent.mkdir(parents=True, exist_ok=True)+ with _make_db(db_path) as conn:+ _add_session(conn, "shared", title, question=title)+ monkeypatch.setattr("deeptutor.multi_user.paths.get_account_path_service", lambda: account)+ monkeypatch.setattr(adapters, "get_account_path_service", lambda: account)+ monkeypatch.setattr(+ "deeptutor.services.workspace.get_content_workspace_service",+ lambda: SimpleNamespace(+ registered_bindings=lambda: [_binding("custom", root)],+ binding_by_id=lambda _: _binding("custom", root),+ ),+ )++ with workspace_context(scope):+ assert adapters.get_path_service().get_chat_history_db() == custom.get_chat_history_db()+ chats = adapters.read_chat_entities()+ assert {e.id: e.label for e in chats} == {"shared": "Default", "ws:custom:shared": "Custom"}+ assert {e.id for e in adapters.probe_chat_entities()} == {"shared", "ws:custom:shared"}+ assert {e.id for e in adapters.read_quiz_entities()} == {"shared:q1", "ws:custom:shared:q1"}