semantics: complete DEF-to-region migration, fix regressions
- Convert legacy [DEF🆔Type] anchors to #region/#endregion across 329 files
- Reinstate _normalize_timestamp_value in sql_generator.py
- Fix MarkerLogger→logger migration in events.py (molecular CoT markers)
- Fix dataset_review orchestrator dependencies (_build_execution_snapshot)
- Fix config_manager stale-record deletion (moved to save path only)
- Add 77 missing [/DEF:] closers in 5 unbalanced test files
- Update assistant_chat.integration.test.js for #region format
- Apply molecular-cot-logging markers (REASON/REFLECT/EXPLORE) via logger.* methods
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# #region GitServiceBase [C:3] [TYPE Module] [SEMANTICS git, service, repository, version_control, initialization]
|
||||
# @LAYER: Infra
|
||||
# @BRIEF Core GitService base class — initialization, path resolution, repo lifecycle (init/delete/get), and identity configuration.
|
||||
# @LAYER Infra
|
||||
# @RELATION INHERITED_BY -> [GitService]
|
||||
# @RELATION DEPENDS_ON -> [SessionLocal]
|
||||
# @RELATION DEPENDS_ON -> [AppConfigRecord]
|
||||
@@ -14,10 +14,7 @@ from typing import Any, Dict, List, Optional
|
||||
from git import Repo
|
||||
from git.exc import InvalidGitRepositoryError, NoSuchPathError
|
||||
from fastapi import HTTPException
|
||||
from src.core.logger import belief_scope
|
||||
from src.core.cot_logger import MarkerLogger
|
||||
|
||||
log = MarkerLogger("GitBase")
|
||||
from src.core.logger import logger, belief_scope
|
||||
from src.models.git import GitRepository
|
||||
from src.models.config import AppConfigRecord
|
||||
from src.core.database import SessionLocal
|
||||
@@ -26,8 +23,8 @@ from src.core.database import SessionLocal
|
||||
# #region GitServiceBase [C:3] [TYPE Class]
|
||||
# @BRIEF Base class for GitService providing initialization, path resolution, repository lifecycle, and identity.
|
||||
class GitServiceBase:
|
||||
# #region GitService_init [TYPE Function]
|
||||
# @BRIEF Initializes the GitService with a base path for repositories.
|
||||
# [DEF:GitService_init:Function]
|
||||
# @PURPOSE: Initializes the GitService with a base path for repositories.
|
||||
# @PARAM: base_path (str) - Root directory for all Git clones.
|
||||
# @PRE: base_path is a valid string path.
|
||||
# @POST: GitService is initialized; base_path directory exists.
|
||||
@@ -38,12 +35,12 @@ class GitServiceBase:
|
||||
self._uses_default_base_path = base_path == "git_repos"
|
||||
self.base_path = self._resolve_base_path(base_path)
|
||||
self._ensure_base_path_exists()
|
||||
# #endregion GitService_init
|
||||
# [/DEF:GitService_init:Function]
|
||||
|
||||
# #region _ensure_base_path_exists [TYPE Function]
|
||||
# @BRIEF Ensure the repositories root directory exists and is a directory.
|
||||
# @PRE self.base_path is resolved to filesystem path.
|
||||
# @POST self.base_path exists as directory or raises ValueError.
|
||||
# [DEF:_ensure_base_path_exists:Function]
|
||||
# @PURPOSE: Ensure the repositories root directory exists and is a directory.
|
||||
# @PRE: self.base_path is resolved to filesystem path.
|
||||
# @POST: self.base_path exists as directory or raises ValueError.
|
||||
def _ensure_base_path_exists(self) -> None:
|
||||
base = Path(self.base_path)
|
||||
if base.exists() and not base.is_dir():
|
||||
@@ -51,15 +48,17 @@ class GitServiceBase:
|
||||
try:
|
||||
base.mkdir(parents=True, exist_ok=True)
|
||||
except (PermissionError, OSError) as e:
|
||||
log.explore("Cannot create Git repositories base path", payload={"base_path": self.base_path}, error=str(e))
|
||||
logger.warning(
|
||||
f"[_ensure_base_path_exists][Coherence:Failed] Cannot create Git repositories base path: {self.base_path}. Error: {e}"
|
||||
)
|
||||
raise ValueError(f"Cannot create Git repositories base path: {self.base_path}. {e}")
|
||||
# #endregion _ensure_base_path_exists
|
||||
# [/DEF:_ensure_base_path_exists:Function]
|
||||
|
||||
# #region _resolve_base_path [TYPE Function]
|
||||
# @BRIEF Resolve base repository directory from explicit argument or global storage settings.
|
||||
# @PRE base_path is a string path.
|
||||
# @POST Returns absolute path for Git repositories root.
|
||||
# @RETURN str
|
||||
# [DEF:_resolve_base_path:Function]
|
||||
# @PURPOSE: Resolve base repository directory from explicit argument or global storage settings.
|
||||
# @PRE: base_path is a string path.
|
||||
# @POST: Returns absolute path for Git repositories root.
|
||||
# @RETURN: str
|
||||
def _resolve_base_path(self, base_path: str) -> str:
|
||||
backend_root = Path(__file__).parents[3]
|
||||
fallback_path = str((backend_root / base_path).resolve())
|
||||
@@ -86,25 +85,25 @@ class GitServiceBase:
|
||||
return str(repo_root.resolve())
|
||||
return str((root / repo_root).resolve())
|
||||
except Exception as e:
|
||||
log.explore("Falling back to default base path", error=str(e))
|
||||
logger.warning(f"[_resolve_base_path][Coherence:Failed] Falling back to default path: {e}")
|
||||
return fallback_path
|
||||
# #endregion _resolve_base_path
|
||||
# [/DEF:_resolve_base_path:Function]
|
||||
|
||||
# #region _normalize_repo_key [TYPE Function]
|
||||
# @BRIEF Convert user/dashboard-provided key to safe filesystem directory name.
|
||||
# @PRE repo_key can be None/empty.
|
||||
# @POST Returns normalized non-empty key.
|
||||
# @RETURN str
|
||||
# [DEF:_normalize_repo_key:Function]
|
||||
# @PURPOSE: Convert user/dashboard-provided key to safe filesystem directory name.
|
||||
# @PRE: repo_key can be None/empty.
|
||||
# @POST: Returns normalized non-empty key.
|
||||
# @RETURN: str
|
||||
def _normalize_repo_key(self, repo_key: Optional[str]) -> str:
|
||||
raw_key = str(repo_key or "").strip().lower()
|
||||
normalized = re.sub(r"[^a-z0-9._-]+", "-", raw_key).strip("._-")
|
||||
return normalized or "dashboard"
|
||||
# #endregion _normalize_repo_key
|
||||
# [/DEF:_normalize_repo_key:Function]
|
||||
|
||||
# #region _update_repo_local_path [TYPE Function]
|
||||
# @BRIEF Persist repository local_path in GitRepository table when record exists.
|
||||
# @PRE dashboard_id is valid integer.
|
||||
# @POST local_path is updated for existing record.
|
||||
# [DEF:_update_repo_local_path:Function]
|
||||
# @PURPOSE: Persist repository local_path in GitRepository table when record exists.
|
||||
# @PRE: dashboard_id is valid integer.
|
||||
# @POST: local_path is updated for existing record.
|
||||
def _update_repo_local_path(self, dashboard_id: int, local_path: str) -> None:
|
||||
try:
|
||||
session = SessionLocal()
|
||||
@@ -120,21 +119,21 @@ class GitServiceBase:
|
||||
finally:
|
||||
session.close()
|
||||
except Exception as e:
|
||||
log.explore("Failed to update repo local path", error=str(e))
|
||||
# #endregion _update_repo_local_path
|
||||
logger.warning(f"[_update_repo_local_path][Coherence:Failed] {e}")
|
||||
# [/DEF:_update_repo_local_path:Function]
|
||||
|
||||
# #region _migrate_repo_directory [TYPE Function]
|
||||
# @BRIEF Move legacy repository directory to target path and sync DB metadata.
|
||||
# @PRE source_path exists.
|
||||
# @POST Repository content available at target_path.
|
||||
# @RETURN str
|
||||
# [DEF:_migrate_repo_directory:Function]
|
||||
# @PURPOSE: Move legacy repository directory to target path and sync DB metadata.
|
||||
# @PRE: source_path exists.
|
||||
# @POST: Repository content available at target_path.
|
||||
# @RETURN: str
|
||||
def _migrate_repo_directory(self, dashboard_id: int, source_path: str, target_path: str) -> str:
|
||||
source_abs = os.path.abspath(source_path)
|
||||
target_abs = os.path.abspath(target_path)
|
||||
if source_abs == target_abs:
|
||||
return source_abs
|
||||
if os.path.exists(target_abs):
|
||||
log.explore(f"Target already exists, keeping source path: {target_abs}", error="Target path exists")
|
||||
logger.warning(f"[_migrate_repo_directory][Action] Target already exists, keeping source path: {target_abs}")
|
||||
return source_abs
|
||||
Path(target_abs).parent.mkdir(parents=True, exist_ok=True)
|
||||
try:
|
||||
@@ -142,12 +141,14 @@ class GitServiceBase:
|
||||
except OSError:
|
||||
shutil.move(source_abs, target_abs)
|
||||
self._update_repo_local_path(dashboard_id, target_abs)
|
||||
log.reflect(f"Repository migrated for dashboard {dashboard_id}: {source_abs} -> {target_abs}")
|
||||
logger.info(
|
||||
f"[_migrate_repo_directory][Coherence:OK] Repository migrated for dashboard {dashboard_id}: {source_abs} -> {target_abs}"
|
||||
)
|
||||
return target_abs
|
||||
# #endregion _migrate_repo_directory
|
||||
# [/DEF:_migrate_repo_directory:Function]
|
||||
|
||||
# #region _get_repo_path [TYPE Function]
|
||||
# @BRIEF Resolves the local filesystem path for a dashboard's repository.
|
||||
# [DEF:_get_repo_path:Function]
|
||||
# @PURPOSE: Resolves the local filesystem path for a dashboard's repository.
|
||||
# @PARAM: dashboard_id (int)
|
||||
# @PARAM: repo_key (Optional[str]) - Slug-like key used when DB local_path is absent.
|
||||
# @PRE: dashboard_id is an integer.
|
||||
@@ -183,17 +184,17 @@ class GitServiceBase:
|
||||
return self._migrate_repo_directory(dashboard_id, db_path, target_path)
|
||||
return db_path
|
||||
except Exception as e:
|
||||
log.explore("Could not resolve local_path from DB", error=str(e))
|
||||
logger.warning(f"[_get_repo_path][Coherence:Failed] Could not resolve local_path from DB: {e}")
|
||||
legacy_id_path = os.path.join(self.legacy_base_path, str(dashboard_id))
|
||||
if os.path.exists(legacy_id_path) and not os.path.exists(target_path):
|
||||
return self._migrate_repo_directory(dashboard_id, legacy_id_path, target_path)
|
||||
if os.path.exists(target_path):
|
||||
self._update_repo_local_path(dashboard_id, target_path)
|
||||
return target_path
|
||||
# #endregion _get_repo_path
|
||||
# [/DEF:_get_repo_path:Function]
|
||||
|
||||
# #region init_repo [TYPE Function]
|
||||
# @BRIEF Initialize or clone a repository for a dashboard.
|
||||
# [DEF:init_repo:Function]
|
||||
# @PURPOSE: Initialize or clone a repository for a dashboard.
|
||||
# @PARAM: dashboard_id (int), remote_url (str), pat (str), repo_key (Optional[str]).
|
||||
# @PRE: dashboard_id is int, remote_url is valid Git URL, pat is provided.
|
||||
# @POST: Repository is cloned or opened at the local path.
|
||||
@@ -209,11 +210,11 @@ class GitServiceBase:
|
||||
else:
|
||||
auth_url = remote_url
|
||||
if os.path.exists(repo_path):
|
||||
log.reason(f"Opening existing repo at {repo_path}")
|
||||
logger.info(f"[init_repo][Action] Opening existing repo at {repo_path}")
|
||||
try:
|
||||
repo = Repo(repo_path)
|
||||
except (InvalidGitRepositoryError, NoSuchPathError):
|
||||
log.explore(f"Existing path is not a Git repository, recreating: {repo_path}", error="Not a git repository")
|
||||
logger.warning(f"[init_repo][Action] Existing path is not a Git repository, recreating: {repo_path}")
|
||||
stale_path = Path(repo_path)
|
||||
if stale_path.exists():
|
||||
shutil.rmtree(stale_path, ignore_errors=True)
|
||||
@@ -225,16 +226,16 @@ class GitServiceBase:
|
||||
repo = Repo.clone_from(auth_url, repo_path)
|
||||
self._ensure_gitflow_branches(repo, dashboard_id)
|
||||
return repo
|
||||
log.reason(f"Cloning {remote_url} to {repo_path}")
|
||||
logger.info(f"[init_repo][Action] Cloning {remote_url} to {repo_path}")
|
||||
repo = Repo.clone_from(auth_url, repo_path)
|
||||
self._ensure_gitflow_branches(repo, dashboard_id)
|
||||
return repo
|
||||
# #endregion init_repo
|
||||
# [/DEF:init_repo:Function]
|
||||
|
||||
# #region delete_repo [TYPE Function]
|
||||
# @BRIEF Remove local repository and DB binding for a dashboard.
|
||||
# @PRE dashboard_id is a valid integer.
|
||||
# @POST Local path is deleted when present and GitRepository row is removed.
|
||||
# [DEF:delete_repo:Function]
|
||||
# @PURPOSE: Remove local repository and DB binding for a dashboard.
|
||||
# @PRE: dashboard_id is a valid integer.
|
||||
# @POST: Local path is deleted when present and GitRepository row is removed.
|
||||
def delete_repo(self, dashboard_id: int) -> None:
|
||||
with belief_scope("GitService.delete_repo"):
|
||||
repo_path = self._get_repo_path(dashboard_id)
|
||||
@@ -267,34 +268,34 @@ class GitServiceBase:
|
||||
raise
|
||||
except Exception as e:
|
||||
session.rollback()
|
||||
log.explore(f"Failed to delete repository for dashboard {dashboard_id}", error=str(e))
|
||||
logger.error(f"[delete_repo][Coherence:Failed] Failed to delete repository for dashboard {dashboard_id}: {e}")
|
||||
raise HTTPException(status_code=500, detail=f"Failed to delete repository: {str(e)}")
|
||||
finally:
|
||||
session.close()
|
||||
# #endregion delete_repo
|
||||
# [/DEF:delete_repo:Function]
|
||||
|
||||
# #region get_repo [TYPE Function]
|
||||
# @BRIEF Get Repo object for a dashboard.
|
||||
# @PRE Repository must exist on disk for the given dashboard_id.
|
||||
# @POST Returns a GitPython Repo instance for the dashboard.
|
||||
# @RETURN Repo
|
||||
# [DEF:get_repo:Function]
|
||||
# @PURPOSE: Get Repo object for a dashboard.
|
||||
# @PRE: Repository must exist on disk for the given dashboard_id.
|
||||
# @POST: Returns a GitPython Repo instance for the dashboard.
|
||||
# @RETURN: Repo
|
||||
def get_repo(self, dashboard_id: int) -> Repo:
|
||||
with belief_scope("GitService.get_repo"):
|
||||
repo_path = self._get_repo_path(dashboard_id)
|
||||
if not os.path.exists(repo_path):
|
||||
log.explore(f"Repository for dashboard {dashboard_id} does not exist", error="Repository not found")
|
||||
logger.error(f"[get_repo][Coherence:Failed] Repository for dashboard {dashboard_id} does not exist")
|
||||
raise HTTPException(status_code=404, detail=f"Repository for dashboard {dashboard_id} not found")
|
||||
try:
|
||||
return Repo(repo_path)
|
||||
except Exception as e:
|
||||
log.explore(f"Failed to open repository at {repo_path}", error=str(e))
|
||||
logger.error(f"[get_repo][Coherence:Failed] Failed to open repository at {repo_path}: {e}")
|
||||
raise HTTPException(status_code=500, detail="Failed to open local Git repository")
|
||||
# #endregion get_repo
|
||||
# [/DEF:get_repo:Function]
|
||||
|
||||
# #region configure_identity [TYPE Function]
|
||||
# @BRIEF Configure repository-local Git committer identity for user-scoped operations.
|
||||
# @PRE dashboard_id repository exists; git_username/git_email may be empty.
|
||||
# @POST Repository config has user.name and user.email when both identity values are provided.
|
||||
# [DEF:configure_identity:Function]
|
||||
# @PURPOSE: Configure repository-local Git committer identity for user-scoped operations.
|
||||
# @PRE: dashboard_id repository exists; git_username/git_email may be empty.
|
||||
# @POST: Repository config has user.name and user.email when both identity values are provided.
|
||||
def configure_identity(self, dashboard_id: int, git_username: Optional[str], git_email: Optional[str]) -> None:
|
||||
with belief_scope("GitService.configure_identity"):
|
||||
normalized_username = str(git_username or "").strip()
|
||||
@@ -306,10 +307,10 @@ class GitServiceBase:
|
||||
with repo.config_writer(config_level="repository") as config_writer:
|
||||
config_writer.set_value("user", "name", normalized_username)
|
||||
config_writer.set_value("user", "email", normalized_email)
|
||||
log.reason(f"Applied repository-local git identity for dashboard {dashboard_id}")
|
||||
logger.info("[configure_identity][Action] Applied repository-local git identity for dashboard %s", dashboard_id)
|
||||
except Exception as e:
|
||||
log.explore("Failed to configure git identity", error=str(e))
|
||||
logger.error(f"[configure_identity][Coherence:Failed] Failed to configure git identity: {e}")
|
||||
raise HTTPException(status_code=500, detail=f"Failed to configure git identity: {str(e)}")
|
||||
# #endregion configure_identity
|
||||
# [/DEF:configure_identity:Function]
|
||||
# #endregion GitServiceBase
|
||||
# #endregion GitServiceBase
|
||||
|
||||
Reference in New Issue
Block a user