From 33c95086f629f06bb2beebc4db7ecac63efea2c2 Mon Sep 17 00:00:00 2001 From: "Bingxi Zhao (Frank)" <13051345080@163.com> Date: Wed, 7 Oct 2026 20:19:57 +0800 Subject: [PATCH] refactor(runtime): share home-dependent cache reset implementation --- deeptutor/runtime/cache_reset.py | 43 ++++++++++++++++++++++++++++++++ deeptutor/runtime/launcher.py | 37 +++------------------------ deeptutor_cli/init_cmd.py | 42 +++---------------------------- 3 files changed, 49 insertions(+), 73 deletions(-) create mode 100644 deeptutor/runtime/cache_reset.py 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):