Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

refactor(runtime): share home-dependent cache reset implementation

Bingxi Zhao (Frank)33c95086f629
diff --git a/deeptutor/runtime/cache_reset.py b/deeptutor/runtime/cache_reset.py
new file mode 100644
index 000000000..4c6c000f3
--- /dev/null
+++ b/deeptutor/runtime/cache_reset.py
@@ -0,0 +1,43 @@
+"""Shared cache reset after selecting a runtime home.
+
+Service imports remain lazy so CLI/bootstrap can select DEEPTUTOR_HOME first.
+"""
+
+from __future__ import annotations
+
+import logging
+
+logger = logging.getLogger(__name__)
+
+
+def reset_runtime_singletons() -> None:
+ """Refresh home-dependent caches, reporting failures without skipping peers."""
+ try:
+ from deeptutor.services.path_service import PathService
+
+ PathService.reset_instance()
+ except Exception:
+ logger.exception(
+ "PathService reset failed; the runtime may keep writing to the "
+ "previous DEEPTUTOR_HOME instead of the selected one"
+ )
+ try:
+ from deeptutor.services.config.runtime_settings import RuntimeSettingsService
+
+ RuntimeSettingsService._instances.clear()
+ except Exception:
+ logger.warning(
+ "RuntimeSettings cache reset failed; settings cached for the "
+ "previous DEEPTUTOR_HOME may be reused",
+ exc_info=True,
+ )
+ try:
+ from deeptutor.services.config.model_catalog import ModelCatalogService
+
+ ModelCatalogService._instances.clear()
+ except Exception:
+ logger.warning(
+ "ModelCatalog cache reset failed; model catalog cached for the "
+ "previous DEEPTUTOR_HOME may be reused",
+ exc_info=True,
+ )
diff --git a/deeptutor/runtime/launcher.py b/deeptutor/runtime/launcher.py
index 122d4543b..91e28997a 100644
--- a/deeptutor/runtime/launcher.py
+++ b/deeptutor/runtime/launcher.py
@@ -141,41 +141,10 @@ def _log(message: str) -> None:
def _reset_runtime_singletons() -> None:
- """Make a just-selected DEEPTUTOR_HOME visible to path/config singletons.
+ """Compatibility entry point for runtime-home selection."""
+ from deeptutor.runtime.cache_reset import reset_runtime_singletons
- Same caveat as ``deeptutor_cli.init_cmd``: a reset that silently fails
- leaves the previous home's cached paths in place, so failures are logged
- instead of swallowed.
- """
- try:
- from deeptutor.services.path_service import PathService
-
- PathService.reset_instance()
- except Exception:
- logger.exception(
- "PathService reset failed; the runtime may keep writing to the "
- "previous DEEPTUTOR_HOME instead of the selected one"
- )
- try:
- from deeptutor.services.config.runtime_settings import RuntimeSettingsService
-
- RuntimeSettingsService._instances.clear()
- except Exception:
- logger.warning(
- "RuntimeSettings cache reset failed; settings cached for the "
- "previous DEEPTUTOR_HOME may be reused",
- exc_info=True,
- )
- try:
- from deeptutor.services.config.model_catalog import ModelCatalogService
-
- ModelCatalogService._instances.clear()
- except Exception:
- logger.warning(
- "ModelCatalog cache reset failed; model catalog cached for the "
- "previous DEEPTUTOR_HOME may be reused",
- exc_info=True,
- )
+ reset_runtime_singletons()
def _get_pgid(pid: int | None) -> int | None:
diff --git a/deeptutor_cli/init_cmd.py b/deeptutor_cli/init_cmd.py
index ec5474bb4..dad1a4007 100644
--- a/deeptutor_cli/init_cmd.py
+++ b/deeptutor_cli/init_cmd.py
@@ -10,7 +10,6 @@ intentionally thin so the order of steps is easy to read top-to-bottom.
from __future__ import annotations
-import logging
from pathlib import Path
from typing import Any
@@ -21,47 +20,12 @@ from deeptutor.runtime.home import DEEPTUTOR_HOME_ENV, get_runtime_home
from . import init_wizard as wiz
-logger = logging.getLogger(__name__)
-
def _reset_runtime_singletons() -> None:
- """Drop cached service instances so the new DEEPTUTOR_HOME takes effect.
+ """Compatibility entry point for runtime-home selection."""
+ from deeptutor.runtime.cache_reset import reset_runtime_singletons
- ``deeptutor init`` may pass ``--home`` to target a different workspace; the
- singletons cache paths from the *previous* PathService and will silently
- write to the wrong place if not cleared. Failures are therefore logged
- instead of swallowed: a silent reset failure means every later write still
- lands in the old workspace with no trace of why.
- """
- try:
- from deeptutor.services.path_service import PathService
-
- PathService.reset_instance()
- except Exception:
- logger.exception(
- "PathService reset failed; later writes may keep going to the "
- "previous DEEPTUTOR_HOME instead of the selected one"
- )
- try:
- from deeptutor.services.config.runtime_settings import RuntimeSettingsService
-
- RuntimeSettingsService._instances.clear()
- except Exception:
- logger.warning(
- "RuntimeSettings cache reset failed; settings cached for the "
- "previous DEEPTUTOR_HOME may be reused",
- exc_info=True,
- )
- try:
- from deeptutor.services.config.model_catalog import ModelCatalogService
-
- ModelCatalogService._instances.clear()
- except Exception:
- logger.warning(
- "ModelCatalog cache reset failed; model catalog cached for the "
- "previous DEEPTUTOR_HOME may be reused",
- exc_info=True,
- )
+ reset_runtime_singletons()
def _ensure_model_service(catalog: dict, service_name: str, profile_id: str, model_id: str):