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.

5.1 KB
Raw Blame
#!/usr/bin/env python3
"""
Pre-commit hook to check for incorrect research_id type hints.
Research IDs are UUIDs and should always be treated as strings, never as integers.
"""

import sys
import re
import os
from pathlib import Path

_REPO_ROOT = Path(__file__).resolve().parent.parent

# Patterns to check for, as (id, regex, message). The id is what a file may be
# sanctioned for below — exemptions are per-pattern, never whole-file, so a
# sanctioned boundary file still gets every other check.
_PATTERNS = [
    # Flask route with int type
    (
        "route-int",
        r"<int:research_id>",
        "Flask route uses <int:research_id> - should be <string:research_id>",
    ),
    # Type hints with int
    (
        "int-hint",
        r"research_id:\s*int",
        "Type hint uses research_id: int - should be research_id: str",
    ),
    # Function parameters with int conversion
    (
        "int-cast",
        r"int\(research_id\)",
        "Converting research_id to int - research IDs are UUIDs/strings",
    ),
    # Integer comparison patterns
    (
        "int-compare",
        r"research_id\s*==\s*\d+",
        "Comparing research_id to integer - research IDs are UUIDs/strings",
    ),
]

# The integer-research-id boundary, as repo-relative POSIX paths mapped to the
# pattern ids each file is allowed to trip. Research IDs are UUID strings
# everywhere except at the sanctioned benchmark boundary: ``BenchmarkRun.id``
# is a per-user autoincrement integer, so the socket subscription key composes
# the owner with an ``int`` benchmark id (see ``__subscription_key``).
#
# Anchored to the FULL repo-relative path, not the basename, so the repo-wide
# ban cannot be defeated by picking a filename. Exemptions are per-pattern:
# The socket layer may cast a numeric benchmark id to int, but a stray
# ``research_id: int`` hint or ``<int:research_id>`` route there still fails.
#
# Upstream names web/services/socket_service.py (the Flask socket service).
# This branch replaced that with the ASGI socketio_asgi.py, which carries the
# same sanctioned cast inside ``_subscription_key`` -- so the exemption moves
# with it. Left pointing at the deleted path, the hook fails on the ported
# file instead.
_SANCTIONED_INT_BOUNDARY = {
    "src/local_deep_research/web/services/socketio_asgi.py": frozenset(
        {"int-cast"}
    ),
}


def _repo_rel_posix(filepath):
    """Repo-relative POSIX path for ``filepath``, or ``None`` if outside repo."""
    try:
        return Path(filepath).resolve().relative_to(_REPO_ROOT).as_posix()
    except ValueError:
        return None


def check_file(filepath):
    """Check a single file for incorrect research_id patterns."""
    errors = []

    with open(filepath, "r", encoding="utf-8") as f:
        lines = f.readlines()

    sanctioned = _SANCTIONED_INT_BOUNDARY.get(
        _repo_rel_posix(filepath), frozenset()
    )

    for line_num, line in enumerate(lines, 1):
        # Skip comment and docstring lines — comments like
        # "# Flask route: <int:research_id> (old API)" should not fire.
        if line.lstrip().startswith(("#", '"""', "'''")):
            continue
        for pattern_id, pattern, message in _PATTERNS:
            if pattern_id in sanctioned:
                continue
            if re.search(pattern, line):
                errors.append(f"{filepath}:{line_num}: {message}")
                errors.append(f"  {line.strip()}")

    return errors


def main():
    """Main entry point."""
    # Allow unencrypted databases when the hook runs standalone (dev
    # machines may still have a legacy unencrypted DB). Kept out of module
    # scope so importing this module — as tests/ does — cannot mutate the
    # process environment.
    os.environ["LDR_ALLOW_UNENCRYPTED"] = "true"

    # Get files to check from command line arguments
    files_to_check = sys.argv[1:]

    if not files_to_check:
        print("No files to check")
        return 0

    all_errors = []

    for filepath in files_to_check:
        # Skip non-Python files
        if not filepath.endswith(".py"):
            continue

        # Skip test files, migration files, and pre-commit hooks (they might have legitimate int usage).
        #
        # Previously this used `"test_" in filepath` (bare substring) — that
        # matched production files like protest_handler.py and missed the
        # *_test.py convention and files under a /tests/ directory. Mirror
        # the guard pattern from _is_raw_sql_exempt in custom-checks.py.
        p = Path(filepath)
        if (
            p.name.startswith("test_")
            or p.name.endswith("_test.py")
            or "tests" in p.parts
            or "migration" in filepath.lower()
            or ".pre-commit-hooks" in filepath
        ):
            continue

        errors = check_file(filepath)
        all_errors.extend(errors)

    if all_errors:
        print("Research ID type errors found:")
        print("-" * 80)
        for error in all_errors:
            print(error)
        print("-" * 80)
        print(
            f"Total errors: {len([e for e in all_errors if not e.startswith('  ')])}"
        )
        return 1

    return 0


if __name__ == "__main__":
    sys.exit(main())