Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion code_review_graph/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,33 @@

from __future__ import annotations

import math
import os


def _bounded_float_env(
name: str,
default: float,
*,
lower: float,
upper: float,
) -> float:
"""Read a finite float strictly inside ``(lower, upper)``.

Invalid environment configuration falls back to the documented default
instead of making graph traversal unbounded or failing during import.
"""
raw = os.environ.get(name)
if raw is None:
return default
try:
value = float(raw)
except (TypeError, ValueError):
return default
if not math.isfinite(value) or not lower < value < upper:
return default
return value

SECURITY_KEYWORDS: frozenset[str] = frozenset({
"auth", "login", "password", "token", "session", "crypt", "secret",
"credential", "permission", "sql", "query", "execute", "connect",
Expand All @@ -19,5 +44,30 @@
MAX_BFS_DEPTH = int(os.environ.get("CRG_MAX_BFS_DEPTH", "15"))
MAX_SEARCH_RESULTS = int(os.environ.get("CRG_MAX_SEARCH_RESULTS", "20"))

# BFS engine: "sql" (SQLite recursive CTE) or "networkx" (Python-side BFS)
# Impact traversal engine: "sql" (bounded SQLite relaxation) or "networkx".
BFS_ENGINE = os.environ.get("CRG_BFS_ENGINE", "sql")

# ---------------------------------------------------------------------------
# Impact-radius scoring
# ---------------------------------------------------------------------------
# Each hop multiplies the best score so strongly coupled nodes rank first.
# These review-risk weights intentionally differ from community-clustering
# affinity weights.
IMPACT_EDGE_WEIGHTS: dict[str, float] = {
"CALLS": 1.0,
"INHERITS": 0.9,
"OVERRIDES": 0.9,
"IMPLEMENTS": 0.9,
"TESTED_BY": 0.7,
"REFERENCES": 0.6,
"DEPENDS_ON": 0.6,
"IMPORTS_FROM": 0.5,
"CONTAINS": 0.3,
}
IMPACT_DEFAULT_EDGE_WEIGHT = 0.5
IMPACT_DEPTH_DECAY = _bounded_float_env(
"CRG_IMPACT_DEPTH_DECAY", 0.6, lower=0.0, upper=1.0,
)
IMPACT_SCORE_FLOOR = _bounded_float_env(
"CRG_IMPACT_SCORE_FLOOR", 0.05, lower=0.0, upper=1.0,
)
Loading