Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
fix(resource): isolate watch URI index by account (#3440)
Zayn Jarvise9daae9ebacf
diff --git a/openviking/resource/watch_manager.py b/openviking/resource/watch_manager.pyindex 34c964789..de6a9b499 100644--- a/openviking/resource/watch_manager.py+++ b/openviking/resource/watch_manager.py@@ -165,7 +165,7 @@ class WatchManager:viking_fs: VikingFS instance for persistence storage"""self._tasks: Dict[str, WatchTask] = {}- self._uri_to_task: Dict[str, str] = {}+ self._uri_to_task: Dict[tuple[str, str], str] = {}self._lock = asyncio.Lock()self._viking_fs = viking_fsself._initialized = False@@ -243,7 +243,7 @@ class WatchManager:normalized = Trueself._tasks[task.task_id] = taskif task.to_uri:- self._uri_to_task[task.to_uri] = task.task_id+ self._uri_to_task[(task.account_id, task.to_uri)] = task.task_idexcept Exception as e:logger.warning(f"[WatchManager] Failed to load task {task_data.get('task_id')}: {e}"@@ -345,7 +345,10 @@ class WatchManager:return task.user_id == user_iddef _check_uri_conflict(- self, to_uri: Optional[str], exclude_task_id: Optional[str] = None+ self,+ to_uri: Optional[str],+ account_id: str = "default",+ exclude_task_id: Optional[str] = None,) -> bool:"""Check if target URI conflicts with existing tasks.@@ -359,7 +362,7 @@ class WatchManager:if not to_uri:return False- existing_task_id = self._uri_to_task.get(to_uri)+ existing_task_id = self._uri_to_task.get((account_id, to_uri))if not existing_task_id:return False@@ -409,7 +412,7 @@ class WatchManager:raise ValueError("watch_interval must be > 0")async with self._lock:- if self._check_uri_conflict(to_uri):+ if self._check_uri_conflict(to_uri, account_id=account_id):raise ConflictError(f"Target URI '{to_uri}' is already used by another task",resource=to_uri,@@ -435,7 +438,7 @@ class WatchManager:self._tasks[task.task_id] = taskif to_uri:- self._uri_to_task[to_uri] = task.task_id+ self._uri_to_task[(account_id, to_uri)] = task.task_idawait self._save_tasks()@@ -495,7 +498,9 @@ class WatchManager:f"User {account_id}/{user_id} does not have permission to update task {task_id}")- if self._check_uri_conflict(to_uri, exclude_task_id=task_id):+ if self._check_uri_conflict(+ to_uri, account_id=task.account_id, exclude_task_id=task_id+ ):raise ConflictError(f"Target URI '{to_uri}' is already used by another task",resource=to_uri,@@ -549,9 +554,9 @@ class WatchManager:if to_uri is not None:if old_to_uri and old_to_uri != to_uri:- self._uri_to_task.pop(old_to_uri, None)+ self._uri_to_task.pop((task.account_id, old_to_uri), None)if to_uri:- self._uri_to_task[to_uri] = task_id+ self._uri_to_task[(task.account_id, to_uri)] = task_idawait self._save_tasks()@@ -575,17 +580,18 @@ class WatchManager:self,old_uri: str,new_uri: str,+ account_id: str = "default",) -> Dict[str, str]:old_prefix = old_uri.rstrip("/")new_prefix = new_uri.rstrip("/")plan: Dict[str, str] = {}for task_id, task in self._tasks.items():- if _uri_matches_prefix(task.to_uri, old_prefix):+ if task.account_id == account_id and _uri_matches_prefix(task.to_uri, old_prefix):plan[task_id] = _rewrite_uri_prefix(task.to_uri or "", old_prefix, new_prefix)moving_task_ids = set(plan)for task_id, target_uri in plan.items():- existing_task_id = self._uri_to_task.get(target_uri)+ existing_task_id = self._uri_to_task.get((account_id, target_uri))if existing_task_id and existing_task_id not in moving_task_ids:raise ConflictError(f"Target URI '{target_uri}' is already used by another task",@@ -604,10 +610,11 @@ class WatchManager:new_uri: str,move_resource: Callable[[], Awaitable[None]],rollback_resource: Optional[Callable[[], Awaitable[None]]] = None,+ account_id: str = "default",) -> List[WatchTask]:"""Move a resource and keep watch-task target URIs in sync under one lock."""async with self._lock:- plan = self._plan_move_tasks_under_uri_unlocked(old_uri, new_uri)+ plan = self._plan_move_tasks_under_uri_unlocked(old_uri, new_uri, account_id)move_result = move_resource()if inspect.isawaitable(move_result):await move_result@@ -625,7 +632,7 @@ class WatchManager:for task_id in plan:task = self._tasks[task_id]if task.to_uri:- self._uri_to_task.pop(task.to_uri, None)+ self._uri_to_task.pop((account_id, task.to_uri), None)updated: List[WatchTask] = []for task_id, target_uri in plan.items():@@ -634,7 +641,7 @@ class WatchManager:task.to_uri = target_uriif task.parent_uri is not None and task.parent_uri == old_parent:task.parent_uri = _parent_uri(target_uri)- self._uri_to_task[target_uri] = task_id+ self._uri_to_task[(account_id, target_uri)] = task_idupdated.append(task)await self._save_tasks()@@ -655,13 +662,15 @@ class WatchManager:await rollback_resultraise- async def deactivate_tasks_under_uri_internal(self, uri: str) -> List[WatchTask]:+ async def deactivate_tasks_under_uri_internal(+ self, uri: str, account_id: str = "default"+ ) -> List[WatchTask]:"""Deactivate watch tasks whose target URI is deleted."""async with self._lock:matched = [taskfor task in self._tasks.values()- if _uri_matches_prefix(task.to_uri, uri)+ if task.account_id == account_id and _uri_matches_prefix(task.to_uri, uri)]if not matched:return []@@ -707,7 +716,7 @@ class WatchManager:self._tasks.pop(task_id, None)if task.to_uri:- self._uri_to_task.pop(task.to_uri, None)+ self._uri_to_task.pop((task.account_id, task.to_uri), None)await self._save_tasks()@@ -789,7 +798,7 @@ class WatchManager:WatchTask if found and accessible, None otherwise"""async with self._lock:- task_id = self._uri_to_task.get(to_uri)+ task_id = self._uri_to_task.get((account_id, to_uri))if not task_id:return Nonediff --git a/openviking/service/fs_service.py b/openviking/service/fs_service.pyindex 1ed9782be..57c90bde8 100644--- a/openviking/service/fs_service.py+++ b/openviking/service/fs_service.py@@ -211,7 +211,7 @@ class FSService:refresh_parent_uri = self._semantic_refresh_parent_uri(uri, context_type)memory_overview_uri = self._memory_overview_parent_uri(uri, context_type)result = await viking_fs.rm(uri, recursive=recursive, ctx=ctx)- await self._sync_watch_after_rm(uri, context_type=context_type)+ await self._sync_watch_after_rm(uri, account_id=ctx.account_id, context_type=context_type)queue_status = Nonerequest_registered = Falsetelemetry_id = get_current_telemetry().telemetry_id@@ -408,11 +408,14 @@ class FSService:await watch_manager.sync_tasks_with_resource_move_internal(from_uri,to_uri,+ account_id=ctx.account_id,move_resource=lambda: viking_fs.mv(from_uri, to_uri, ctx=ctx),rollback_resource=lambda: viking_fs.mv(to_uri, from_uri, ctx=ctx),)- async def _sync_watch_after_rm(self, uri: str, *, context_type: str) -> None:+ async def _sync_watch_after_rm(+ self, uri: str, *, account_id: str, context_type: str+ ) -> None:if context_type != "resource":returnif is_watch_task_control_uri(uri):@@ -420,7 +423,7 @@ class FSService:watch_manager = self._get_watch_manager()if not watch_manager:return- deactivated = await watch_manager.deactivate_tasks_under_uri_internal(uri)+ deactivated = await watch_manager.deactivate_tasks_under_uri_internal(uri, account_id)if deactivated:logger.info("Deactivated %d watch task(s) after deleting %s",diff --git a/tests/resource/test_watch_manager.py b/tests/resource/test_watch_manager.pyindex b2bc73dcf..39f5a0f38 100644--- a/tests/resource/test_watch_manager.py+++ b/tests/resource/test_watch_manager.py@@ -233,7 +233,7 @@ class TestWatchManager:)deactivated = await watch_manager_no_fs.deactivate_tasks_under_uri_internal(- "viking://resources/codeask/wiki"+ "viking://resources/codeask/wiki", TEST_ACCOUNT_ID)assert {task.task_id for task in deactivated} == {root.task_id, child.task_id}@@ -261,6 +261,7 @@ class TestWatchManager:"viking://resources/codeask/wiki-renamed",move_resource=move_resource,rollback_resource=rollback_resource,+ account_id=TEST_ACCOUNT_ID,)move_resource.assert_awaited_once()@@ -270,6 +271,38 @@ class TestWatchManager:assert restored.to_uri == "viking://resources/codeask/wiki"assert restored.parent_uri is None+ @pytest.mark.asyncio+ async def test_uri_index_move_and_deactivate_are_account_scoped(self):+ manager = WatchManager()+ uri = "viking://resources/shared"+ task_a = await manager.create_task(+ path="/a", account_id="account-a", to_uri=uri+ )+ task_b = await manager.create_task(+ path="/b", account_id="account-b", to_uri=uri+ )+ with pytest.raises(ConflictError):+ await manager.create_task(path="/duplicate", account_id="account-a", to_uri=uri)++ await manager.sync_tasks_with_resource_move_internal(+ uri,+ f"{uri}-moved",+ move_resource=AsyncMock(),+ account_id="account-a",+ )+ deactivated = await manager.deactivate_tasks_under_uri_internal(uri, "account-b")++ assert task_a.to_uri == f"{uri}-moved"+ assert task_a.is_active is True+ assert task_b.to_uri == uri+ assert task_b.is_active is False+ assert deactivated == [task_b]+ moved = await manager.get_task_by_uri(+ f"{uri}-moved", "account-a", "default", "root"+ )+ assert moved is task_a+ assert await manager.get_task_by_uri(uri, "account-b", "default", "root") is task_b+@pytest.mark.asyncioasync def test_auth_state_persisted_and_hidden_from_public_dict(self, mock_viking_fs: MockVikingFSdiff --git a/tests/service/test_fs_service.py b/tests/service/test_fs_service.pyindex db11cf024..94e3dc988 100644--- a/tests/service/test_fs_service.py+++ b/tests/service/test_fs_service.py@@ -50,17 +50,20 @@ class _FakeWatchManager:self,from_uri,to_uri,+ account_id,move_resource,rollback_resource=None,):- self.sync_calls.append({"from_uri": from_uri, "to_uri": to_uri})+ self.sync_calls.append(+ {"from_uri": from_uri, "to_uri": to_uri, "account_id": account_id}+ )if self.plan_error:raise self.plan_errorawait move_resource()return [SimpleNamespace(task_id="watch-1")]- async def deactivate_tasks_under_uri_internal(self, uri):- self.deactivate_calls.append(uri)+ async def deactivate_tasks_under_uri_internal(self, uri, account_id):+ self.deactivate_calls.append({"uri": uri, "account_id": account_id})return [SimpleNamespace(task_id="watch-1")]@@ -220,7 +223,9 @@ async def test_resource_rm_deactivates_watch_tasks(request_context):await service.rm("viking://resources/codeask/wiki", ctx=request_context, recursive=True)- assert watch_manager.deactivate_calls == ["viking://resources/codeask/wiki"]+ assert watch_manager.deactivate_calls == [+ {"uri": "viking://resources/codeask/wiki", "account_id": "default"}+ ]@pytest.mark.asyncio@@ -256,6 +261,7 @@ async def test_resource_mv_plans_then_moves_then_rewrites_watch_tasks(request_co{"from_uri": "viking://resources/codeask/wiki","to_uri": "viking://resources/codeask/wiki-renamed",+ "account_id": "default",}]assert viking_fs.mv_calls == [