Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

feat: Add custom pre-commit hook for code quality checks

LearningCircuitf46528eb4aab
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 93c8d1fa7a..2eb5b11119 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -21,3 +21,11 @@ repos:
args: [ --fix ]
# Run the formatter.
- id: ruff-format
+ - repo: local
+ hooks:
+ - id: custom-code-checks
+ name: Custom Code Quality Checks
+ entry: .pre-commit-hooks/custom-checks.py
+ language: script
+ files: \.py$
+ description: "Check for loguru usage, logger.exception vs logger.error, and raw SQL"
diff --git a/.pre-commit-hooks/custom-checks.py b/.pre-commit-hooks/custom-checks.py
new file mode 100755
index 0000000000..5690e8c593
--- /dev/null
+++ b/.pre-commit-hooks/custom-checks.py
@@ -0,0 +1,262 @@
+#!/usr/bin/env python3
+"""
+Custom pre-commit hook for Local Deep Research project.
+Checks for:
+1. If loguru is used instead of standard logging
+2. If logger.exception is used instead of logger.error for error handling
+3. That no raw SQL is used, only ORM methods
+"""
+
+import ast
+import sys
+import re
+from typing import List, Tuple
+
+
+class CustomCodeChecker(ast.NodeVisitor):
+ def __init__(self, filename: str):
+ self.filename = filename
+ self.errors = []
+ self.has_loguru_import = False
+ self.has_standard_logging_import = False
+ self.in_except_handler = False
+
+ def visit_Import(self, node):
+ for alias in node.names:
+ if alias.name == "logging":
+ self.has_standard_logging_import = True
+ self.errors.append(
+ (
+ node.lineno,
+ "Use loguru instead of standard logging library",
+ )
+ )
+ elif alias.name == "loguru":
+ self.has_loguru_import = True
+ self.generic_visit(node)
+
+ def visit_ImportFrom(self, node):
+ if node.module == "logging":
+ self.has_standard_logging_import = True
+ self.errors.append(
+ (node.lineno, "Use loguru instead of standard logging library")
+ )
+ elif node.module == "loguru":
+ self.has_loguru_import = True
+ self.generic_visit(node)
+
+ def visit_ExceptHandler(self, node):
+ # Track when we're inside an exception handler
+ old_in_except = self.in_except_handler
+ self.in_except_handler = True
+ self.generic_visit(node)
+ self.in_except_handler = old_in_except
+
+ def visit_Call(self, node):
+ # Check for logger.error usage in exception handlers (should use logger.exception instead)
+ if (
+ isinstance(node.func, ast.Attribute)
+ and node.func.attr == "error"
+ and isinstance(node.func.value, ast.Name)
+ and node.func.value.id == "logger"
+ and self.in_except_handler
+ ):
+ self.errors.append(
+ (
+ node.lineno,
+ "Use logger.exception() instead of logger.error() in exception handlers",
+ )
+ )
+ self.generic_visit(node)
+
+
+def check_raw_sql(content: str, filename: str) -> List[Tuple[int, str]]:
+ """Check for raw SQL usage patterns."""
+ errors = []
+ lines = content.split("\n")
+
+ # Skip checking this file itself (contains regex patterns that look like SQL)
+ if "custom-checks.py" in filename:
+ return errors
+
+ # More specific patterns for database execute calls to avoid false positives
+ db_execute_patterns = [
+ r"cursor\.execute\s*\(", # cursor.execute()
+ r"cursor\.executemany\s*\(", # cursor.executemany()
+ r"conn\.execute\s*\(", # connection.execute()
+ r"connection\.execute\s*\(", # connection.execute()
+ r"session\.execute\s*\(\s*[\"']", # session.execute() with raw SQL string
+ ]
+
+ # SQL statement patterns (only check if they appear to be raw SQL strings)
+ sql_statement_patterns = [
+ r"[\"']\s*SELECT\s+.*FROM\s+", # Raw SELECT in strings
+ r"[\"']\s*INSERT\s+INTO\s+", # Raw INSERT in strings
+ r"[\"']\s*UPDATE\s+.*SET\s+", # Raw UPDATE in strings
+ r"[\"']\s*DELETE\s+FROM\s+", # Raw DELETE in strings
+ r"[\"']\s*CREATE\s+TABLE\s+", # Raw CREATE TABLE in strings
+ r"[\"']\s*DROP\s+TABLE\s+", # Raw DROP TABLE in strings
+ r"[\"']\s*ALTER\s+TABLE\s+", # Raw ALTER TABLE in strings
+ ]
+
+ # Allowed patterns (ORM usage and legitimate cases)
+ allowed_patterns = [
+ r"session\.query\(",
+ r"\.filter\(",
+ r"\.filter_by\(",
+ r"\.join\(",
+ r"\.order_by\(",
+ r"\.group_by\(",
+ r"\.add\(",
+ r"\.merge\(",
+ r"Query\(",
+ r"relationship\(",
+ r"Column\(",
+ r"Table\(",
+ r"text\(", # SQLAlchemy text() function for raw SQL
+ r"#.*SQL", # Comments mentioning SQL
+ r"\"\"\".*SQL", # Docstrings mentioning SQL
+ r"'''.*SQL", # Docstrings mentioning SQL
+ r"f[\"'].*{", # f-strings (often used for dynamic ORM queries)
+ ]
+
+ for line_num, line in enumerate(lines, 1):
+ line_stripped = line.strip()
+
+ # Skip comments, docstrings, and empty lines
+ if (
+ line_stripped.startswith("#")
+ or line_stripped.startswith('"""')
+ or line_stripped.startswith("'''")
+ or not line_stripped
+ ):
+ continue
+
+ # Check if line has allowed patterns first
+ has_allowed_pattern = any(
+ re.search(pattern, line, re.IGNORECASE)
+ for pattern in allowed_patterns
+ )
+
+ if has_allowed_pattern:
+ continue
+
+ # Check for database execute patterns
+ for pattern in db_execute_patterns:
+ if re.search(pattern, line, re.IGNORECASE):
+ # Check if this might be acceptable (in migrations or tests)
+ is_migration = (
+ "migration" in filename.lower()
+ or "alembic" in filename.lower()
+ or "/migrations/" in filename
+ )
+ is_test = "test" in filename.lower()
+
+ # Allow raw SQL in migrations and some test contexts
+ if not (is_migration or (is_test and "schema" in line.lower())):
+ errors.append(
+ (
+ line_num,
+ f"Raw SQL execute detected: '{line_stripped[:50]}...'. Use ORM methods instead.",
+ )
+ )
+
+ # Check for SQL statement patterns
+ for pattern in sql_statement_patterns:
+ if re.search(pattern, line, re.IGNORECASE):
+ # Check if this might be acceptable (in migrations or tests)
+ is_migration = (
+ "migration" in filename.lower()
+ or "alembic" in filename.lower()
+ or "/migrations/" in filename
+ )
+ is_test = "test" in filename.lower()
+
+ # Allow raw SQL in migrations and some test contexts
+ if not (is_migration or (is_test and "schema" in line.lower())):
+ errors.append(
+ (
+ line_num,
+ f"Raw SQL statement detected: '{line_stripped[:50]}...'. Use ORM methods instead.",
+ )
+ )
+
+ return errors
+
+
+def check_file(filename: str) -> bool:
+ """Check a single Python file for violations."""
+ if not filename.endswith(".py"):
+ return True
+
+ try:
+ with open(filename, "r", encoding="utf-8") as f:
+ content = f.read()
+ except UnicodeDecodeError:
+ # Skip binary files
+ return True
+ except Exception as e:
+ print(f"Error reading {filename}: {e}")
+ return False
+
+ # Parse AST for logging checks
+ try:
+ tree = ast.parse(content, filename=filename)
+ checker = CustomCodeChecker(filename)
+ checker.visit(tree)
+
+ # Check for raw SQL
+ sql_errors = check_raw_sql(content, filename)
+ checker.errors.extend(sql_errors)
+
+ if checker.errors:
+ print(f"\n{filename}:")
+ for line_num, error in checker.errors:
+ print(f" Line {line_num}: {error}")
+ return False
+
+ except SyntaxError:
+ # Skip files with syntax errors (they'll be caught by other tools)
+ pass
+ except Exception as e:
+ print(f"Error parsing {filename}: {e}")
+ return False
+
+ return True
+
+
+def main():
+ """Main function to check all staged Python files."""
+ if len(sys.argv) < 2:
+ print("Usage: custom-checks.py <file1> <file2> ...")
+ sys.exit(1)
+
+ files_to_check = sys.argv[1:]
+ has_errors = False
+
+ print("Running custom code checks...")
+
+ for filename in files_to_check:
+ if not check_file(filename):
+ has_errors = True
+
+ if has_errors:
+ print("\n❌ Custom checks failed. Please fix the issues above.")
+ print("\nGuidelines:")
+ print("1. Use 'from loguru import logger' instead of standard logging")
+ print(
+ "2. Use 'logger.exception()' instead of 'logger.error()' in exception handlers"
+ )
+ print(
+ "3. Use ORM methods instead of raw SQL execute() calls and SQL strings"
+ )
+ print(" - Allowed: session.query(), .filter(), .add(), etc.")
+ print(" - Raw SQL is permitted in migration files and schema tests")
+ sys.exit(1)
+ else:
+ print("✅ All custom checks passed!")
+ sys.exit(0)
+
+
+if __name__ == "__main__":
+ main()