diff --git a/src/basic_memory/index/watch_service.py b/src/basic_memory/index/watch_service.py index fb78a771a..b61041bb2 100644 --- a/src/basic_memory/index/watch_service.py +++ b/src/basic_memory/index/watch_service.py @@ -357,6 +357,17 @@ async def _select_projects_to_watch(self) -> list[Project]: projects = [project for project in projects if project.name not in skipped] logger.debug(f"Skipping projects that are not locally syncable: {skipped}") + # Trigger: a configured project's directory does not exist (deleted, or an + # unmounted drive). + # Why: watchfiles refuses the whole path list when one path is missing, so a + # single absent directory stopped file watching for every project. + # Outcome: watch the projects that exist; the next reload picks the missing + # one up once its directory is back. + missing = [project.name for project in projects if not Path(project.path).is_dir()] + if missing: + projects = [project for project in projects if project.name not in missing] + logger.warning(f"Not watching projects whose directory is missing: {missing}") + return list(projects) async def run(self) -> None: # pragma: no cover diff --git a/tests/index/test_watch_new_projects.py b/tests/index/test_watch_new_projects.py index 2b301e5b1..18f028db1 100644 --- a/tests/index/test_watch_new_projects.py +++ b/tests/index/test_watch_new_projects.py @@ -67,6 +67,42 @@ async def test_watch_selection_rereads_added_and_removed_project_config( assert [project.id for project in await watcher._select_projects_to_watch()] == [added.id] +@pytest.mark.asyncio +async def test_a_missing_project_directory_does_not_stop_watching_the_others( + app_config, config_manager, project_repository, session_maker, test_project, tmp_path_factory +): + # watchfiles refuses the whole path list when one path is missing, so a project + # whose directory is gone must drop out of the selection, not stop the watch. + # Loading config recreates missing config paths, so the stale path here is the + # project row's, as when the row and the config drift apart. + gone = await _register_project( + "gone-project", + tmp_path_factory.mktemp("gone-watch-root"), + config_manager, + project_repository, + session_maker, + ) + gone_root = tmp_path_factory.mktemp("gone-row-root") + gone_root.rmdir() + async with db.scoped_session(session_maker) as session: + await project_repository.update(session, gone.id, {"path": str(gone_root)}) + watcher = WatchService(app_config.model_copy(deep=True), project_repository, session_maker) + + selected = await watcher._select_projects_to_watch() + assert [project.id for project in selected] == [test_project.id] + + stop_event = asyncio.Event() + stop_event.set() + async for _ in watch_module.awatch( + *(project.path for project in selected), stop_event=stop_event + ): + pass + + gone_root.mkdir() + selected = await watcher._select_projects_to_watch() + assert {project.id for project in selected} == {test_project.id, gone.id} + + @pytest.mark.asyncio async def test_new_project_backfill_runs_after_attach( app_config,