Coverage for src / moai_adk / core / git / event_detector.py: 38.89%

18 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-11-20 20:52 +0900

1""" 

2Event Detector - Identify risky operations. 

3 

4SPEC: .moai/specs/SPEC-CHECKPOINT-EVENT-001/spec.md 

5""" 

6 

7from pathlib import Path 

8 

9 

10class EventDetector: 

11 """Detect potentially risky operations.""" 

12 

13 CRITICAL_FILES = { 

14 "CLAUDE.md", 

15 "config.json", 

16 ".moai/config/config.json", 

17 } 

18 

19 CRITICAL_DIRS = { 

20 ".moai/memory", 

21 } 

22 

23 def is_risky_deletion(self, deleted_files: list[str]) -> bool: 

24 """ 

25 Detect large-scale file deletions. 

26 

27 SPEC requirement: deleting 10 or more files counts as risky. 

28 

29 Args: 

30 deleted_files: Files slated for deletion. 

31 

32 Returns: 

33 True when 10 or more files are deleted, otherwise False. 

34 """ 

35 return len(deleted_files) >= 10 

36 

37 def is_risky_refactoring(self, renamed_files: list[tuple[str, str]]) -> bool: 

38 """ 

39 Detect large-scale refactoring. 

40 

41 SPEC requirement: renaming 10 or more files counts as risky. 

42 

43 Args: 

44 renamed_files: List of (old_name, new_name) pairs. 

45 

46 Returns: 

47 True when 10 or more files are renamed, otherwise False. 

48 """ 

49 return len(renamed_files) >= 10 

50 

51 def is_critical_file(self, file_path: Path) -> bool: 

52 """ 

53 Determine whether the file is critical. 

54 

55 SPEC requirement: modifying CLAUDE.md, config.json, or .moai/memory/*.md is risky. 

56 

57 Args: 

58 file_path: File path to inspect. 

59 

60 Returns: 

61 True when the file is critical, otherwise False. 

62 """ 

63 # Check whether the file name is in the critical list 

64 if file_path.name in self.CRITICAL_FILES: 

65 return True 

66 

67 # Convert to string for further checks 

68 path_str = str(file_path) 

69 

70 # Detect .moai/config/config.json paths 

71 if ".moai/config/config.json" in path_str or ".moai\\config.json" in path_str: 

72 return True 

73 

74 # Detect files inside the .moai/memory/ directory 

75 for critical_dir in self.CRITICAL_DIRS: 

76 if critical_dir in path_str or critical_dir.replace("/", "\\") in path_str: 

77 return True 

78 

79 return False