Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(lint): pick the merge-aware base so in-progress merges are not blamed for base drift

mateo-berri96c8c9cee17b
diff --git a/scripts/ruff_strict_gate.py b/scripts/ruff_strict_gate.py
index 25f6c4d29ba..507077ddf25 100644
--- a/scripts/ruff_strict_gate.py
+++ b/scripts/ruff_strict_gate.py
@@ -18,7 +18,7 @@ import sys
import tempfile
from collections import Counter
from pathlib import Path
-from typing import NamedTuple
+from typing import Final, NamedTuple
REPO_ROOT = Path(__file__).resolve().parent.parent
STRICT_CONFIG = REPO_ROOT / "ruff-strict.toml"
@@ -50,6 +50,25 @@ def _run(cmd: list, cwd: Path = REPO_ROOT) -> str:
return proc.stdout
+def resolve_base_point(base_ref: str, cwd: Path = REPO_ROOT) -> str:
+ """The snapshot commit base counts are measured at: merge-base(base_ref, HEAD),
+ made aware of an in-progress merge. Mid-merge, HEAD is still the pre-merge tip,
+ so its merge-base is the old branch point and every violation the base gained
+ since then would be blamed on this change. While MERGE_HEAD exists, prefer
+ merge-base(base_ref, MERGE_HEAD) whenever it is the newer of the two."""
+ head_point: Final = _run(["git", "merge-base", base_ref, "HEAD"], cwd=cwd).strip()
+ if not head_point:
+ return base_ref
+ merge_head: Final = _run(["git", "rev-parse", "--verify", "--quiet", "MERGE_HEAD"], cwd=cwd).strip()
+ if not merge_head:
+ return head_point
+ merge_point: Final = _run(["git", "merge-base", base_ref, merge_head], cwd=cwd).strip()
+ if not merge_point:
+ return head_point
+ older: Final = _run(["git", "merge-base", head_point, merge_point], cwd=cwd).strip()
+ return merge_point if older == head_point else head_point
+
+
def _ruff_json(cwd: Path, config: Path) -> list:
raw = _run(
["ruff", "check", TARGET, "--config", str(config), "--output-format", "json"],
@@ -135,7 +154,7 @@ def cmd_check(base: str) -> None:
if not over_ceiling(head_counts, budget):
print(f"OK: every strict rule is within its codebase ceiling (base {base})")
return
- base_point = _run(["git", "merge-base", base, "HEAD"]).strip() or base
+ base_point = resolve_base_point(base)
breaches = evaluate(head_counts, base_counts(base_point), budget)
if not breaches:
print(f"OK: every strict rule is within its codebase ceiling (base {base})")
@@ -182,7 +201,7 @@ def cmd_update(base_ref: str = DEFAULT_BASE) -> None:
fixes tighten its own ceilings by exactly what they cleared since it diverged.
"""
budget = json.loads(BUDGET_PATH.read_text())
- base_point = _run(["git", "merge-base", base_ref, "HEAD"]).strip() or base_ref
+ base_point = resolve_base_point(base_ref)
updated = ratcheted_budget(
budget, count_by_rule(head_violations()), base_counts(base_point)
)
diff --git a/scripts/type_check_gate.py b/scripts/type_check_gate.py
index 2c5306cec7d..e35baa99374 100644
--- a/scripts/type_check_gate.py
+++ b/scripts/type_check_gate.py
@@ -42,7 +42,7 @@ import tempfile
from collections import Counter
from collections.abc import Callable, Iterator, Mapping
from pathlib import Path
-from typing import NamedTuple
+from typing import Final, NamedTuple
REPO_ROOT = Path(__file__).resolve().parent.parent
BUDGET_PATH = REPO_ROOT / "basedpyright-code-budget.json"
@@ -107,6 +107,25 @@ def _run(cmd: list[str], cwd: Path = REPO_ROOT) -> str:
return proc.stdout
+def resolve_base_point(base_ref: str, cwd: Path = REPO_ROOT) -> str:
+ """The snapshot commit base counts are measured at: merge-base(base_ref, HEAD),
+ made aware of an in-progress merge. Mid-merge, HEAD is still the pre-merge tip,
+ so its merge-base is the old branch point and every violation the base gained
+ since then would be blamed on this change. While MERGE_HEAD exists, prefer
+ merge-base(base_ref, MERGE_HEAD) whenever it is the newer of the two."""
+ head_point: Final = _run(["git", "merge-base", base_ref, "HEAD"], cwd=cwd).strip()
+ if not head_point:
+ return base_ref
+ merge_head: Final = _run(["git", "rev-parse", "--verify", "--quiet", "MERGE_HEAD"], cwd=cwd).strip()
+ if not merge_head:
+ return head_point
+ merge_point: Final = _run(["git", "merge-base", base_ref, merge_head], cwd=cwd).strip()
+ if not merge_point:
+ return head_point
+ older: Final = _run(["git", "merge-base", head_point, merge_point], cwd=cwd).strip()
+ return merge_point if older == head_point else head_point
+
+
@contextlib.contextmanager
def _temp_worktree(ref: str) -> Iterator[Path]:
parent = Path(tempfile.mkdtemp(prefix="bpr_base_"))
@@ -295,7 +314,7 @@ def cmd_update(current: Mapping[str, int], base_ref: str = DEFAULT_BASE) -> None
by exactly what they cleared since it diverged, and limits never rise.
"""
budget = json.loads(BUDGET_PATH.read_text()) if BUDGET_PATH.exists() else {}
- base_point = _run(["git", "merge-base", base_ref, "HEAD"]).strip() or base_ref
+ base_point = resolve_base_point(base_ref)
updated = ratcheted_budget(budget, current, base_counts_cached(base_point))
BUDGET_PATH.write_text(json.dumps(updated, indent=2, sort_keys=True) + "\n")
cleared = sum(budget[code]["limit"] - updated[code]["limit"] for code in updated)
@@ -321,7 +340,7 @@ def cmd_check(base_ref: str) -> None:
f"OK: every rule is within its basedpyright limit ({sum(head.values())} errors total)"
)
return
- base_point = _run(["git", "merge-base", base_ref, "HEAD"]).strip() or base_ref
+ base_point = resolve_base_point(base_ref)
base = base_counts_cached(base_point)
if is_vacuous_run(base, budget):
print(
diff --git a/scripts/type_discipline_gate.py b/scripts/type_discipline_gate.py
index 10d26dc7b80..cc97ce0f46e 100644
--- a/scripts/type_discipline_gate.py
+++ b/scripts/type_discipline_gate.py
@@ -36,7 +36,7 @@ import sys
import tempfile
from collections import Counter
from pathlib import Path
-from typing import NamedTuple
+from typing import Final, NamedTuple
REPO_ROOT = Path(__file__).resolve().parent.parent
CHECKER = REPO_ROOT / "scripts" / "check_type_discipline.py"
@@ -69,6 +69,25 @@ def _run(cmd: list, cwd: Path = REPO_ROOT) -> str:
return proc.stdout
+def resolve_base_point(base_ref: str, cwd: Path = REPO_ROOT) -> str:
+ """The snapshot commit base counts are measured at: merge-base(base_ref, HEAD),
+ made aware of an in-progress merge. Mid-merge, HEAD is still the pre-merge tip,
+ so its merge-base is the old branch point and every violation the base gained
+ since then would be blamed on this change. While MERGE_HEAD exists, prefer
+ merge-base(base_ref, MERGE_HEAD) whenever it is the newer of the two."""
+ head_point: Final = _run(["git", "merge-base", base_ref, "HEAD"], cwd=cwd).strip()
+ if not head_point:
+ return base_ref
+ merge_head: Final = _run(["git", "rev-parse", "--verify", "--quiet", "MERGE_HEAD"], cwd=cwd).strip()
+ if not merge_head:
+ return head_point
+ merge_point: Final = _run(["git", "merge-base", base_ref, merge_head], cwd=cwd).strip()
+ if not merge_point:
+ return head_point
+ older: Final = _run(["git", "merge-base", head_point, merge_point], cwd=cwd).strip()
+ return merge_point if older == head_point else head_point
+
+
def _check(root: Path, checker: Path) -> list:
# Resolve root first: on macOS tempfile dirs (/var/...) resolve to /private/var/...,
# and the checker prints already-resolved absolute paths, so relative_to would fail.
@@ -160,7 +179,7 @@ def cmd_check(base: str) -> None:
if not over_ceiling(head_counts, budget):
print(f"OK: every LIT rule is within its codebase ceiling (base {base})")
return
- base_point = _run(["git", "merge-base", base, "HEAD"]).strip() or base
+ base_point = resolve_base_point(base)
breaches = evaluate(head_counts, base_counts(base_point), budget)
if not breaches:
print(f"OK: every LIT rule is within its codebase ceiling (base {base})")
@@ -225,7 +244,7 @@ def cmd_update(base_ref: str = DEFAULT_BASE) -> None:
fixes tighten its own ceilings by exactly what they cleared since it diverged.
"""
budget = json.loads(BUDGET_PATH.read_text())
- base_point = _run(["git", "merge-base", base_ref, "HEAD"]).strip() or base_ref
+ base_point = resolve_base_point(base_ref)
seeded = frozenset(budget) - _base_budget_rules(base_point)
updated = ratcheted_budget(
budget, count_by_rule(head_violations()), base_counts(base_point), seeded
diff --git a/tests/test_litellm/test_ruff_strict_gate.py b/tests/test_litellm/test_ruff_strict_gate.py
index aad0e1bc9f9..abdeb6feecc 100644
--- a/tests/test_litellm/test_ruff_strict_gate.py
+++ b/tests/test_litellm/test_ruff_strict_gate.py
@@ -1,4 +1,5 @@
import importlib.util
+import subprocess
from pathlib import Path
import pytest
@@ -110,3 +111,43 @@ def test_over_ceiling_ignores_rules_missing_from_the_budget():
def test_over_ceiling_is_independent_across_rules():
budget = {**rule("ANN001", 150), **rule("C901", 10)}
assert gate.over_ceiling({"ANN001": 130, "C901": 11}, budget) == frozenset({"C901"})
+
+
+def _git(cwd, *args):
+ proc = subprocess.run(["git", *args], cwd=cwd, capture_output=True, text=True)
+ assert proc.returncode == 0, proc.stderr
+ return proc.stdout.strip()
+
+
+def _commit(cwd, name):
+ (cwd / name).write_text(name)
+ _git(cwd, "add", "-A")
+ _git(cwd, "commit", "-q", "-m", name)
+ return _git(cwd, "rev-parse", "HEAD")
+
+
+def _branched_repo(tmp_path):
+ repo = tmp_path / "repo"
+ repo.mkdir()
+ _git(repo, "init", "-q", "-b", "main")
+ _git(repo, "config", "user.email", "[email protected]")
+ _git(repo, "config", "user.name", "gate")
+ _git(repo, "config", "commit.gpgsign", "false")
+ branch_point = _commit(repo, "shared.txt")
+ _git(repo, "checkout", "-q", "-b", "feature")
+ _commit(repo, "feature.txt")
+ _git(repo, "checkout", "-q", "main")
+ base_tip = _commit(repo, "drift.txt")
+ _git(repo, "checkout", "-q", "feature")
+ return repo, branch_point, base_tip
+
+
+def test_base_point_is_the_branch_point_when_no_merge_is_in_progress(tmp_path):
+ repo, branch_point, _ = _branched_repo(tmp_path)
+ assert gate.resolve_base_point("main", cwd=repo) == branch_point
+
+
+def test_base_point_mid_merge_advances_to_the_merged_in_base_tip(tmp_path):
+ repo, _, base_tip = _branched_repo(tmp_path)
+ _git(repo, "merge", "--no-commit", "--no-ff", "main")
+ assert gate.resolve_base_point("main", cwd=repo) == base_tip
diff --git a/tests/test_litellm/test_type_check_gate.py b/tests/test_litellm/test_type_check_gate.py
index 66a28360af9..cce980a1c3d 100644
--- a/tests/test_litellm/test_type_check_gate.py
+++ b/tests/test_litellm/test_type_check_gate.py
@@ -1,5 +1,6 @@
import importlib.util
import json
+import subprocess
from pathlib import Path
_MODULE_PATH = Path(__file__).resolve().parents[2] / "scripts" / "type_check_gate.py"
@@ -287,3 +288,61 @@ def test_an_empty_base_pass_is_never_cached(tmp_path):
assert gate.base_counts_cached("abc123", cache_dir=tmp_path, compute=crashed) == {}
assert calls == ["abc123", "abc123"]
assert list(tmp_path.iterdir()) == []
+
+
+def _git(cwd, *args):
+ proc = subprocess.run(["git", *args], cwd=cwd, capture_output=True, text=True)
+ assert proc.returncode == 0, proc.stderr
+ return proc.stdout.strip()
+
+
+def _commit(cwd, name):
+ (cwd / name).write_text(name)
+ _git(cwd, "add", "-A")
+ _git(cwd, "commit", "-q", "-m", name)
+ return _git(cwd, "rev-parse", "HEAD")
+
+
+def _init_repo(tmp_path):
+ repo = tmp_path / "repo"
+ repo.mkdir()
+ _git(repo, "init", "-q", "-b", "main")
+ _git(repo, "config", "user.email", "[email protected]")
+ _git(repo, "config", "user.name", "gate")
+ _git(repo, "config", "commit.gpgsign", "false")
+ return repo
+
+
+def _branched_repo(tmp_path):
+ repo = _init_repo(tmp_path)
+ branch_point = _commit(repo, "shared.txt")
+ _git(repo, "checkout", "-q", "-b", "feature")
+ _commit(repo, "feature.txt")
+ _git(repo, "checkout", "-q", "main")
+ base_tip = _commit(repo, "drift.txt")
+ _git(repo, "checkout", "-q", "feature")
+ return repo, branch_point, base_tip
+
+
+def test_base_point_is_the_branch_point_when_no_merge_is_in_progress(tmp_path):
+ repo, branch_point, _ = _branched_repo(tmp_path)
+ assert gate.resolve_base_point("main", cwd=repo) == branch_point
+
+
+def test_base_point_mid_merge_advances_to_the_merged_in_base_tip(tmp_path):
+ repo, _, base_tip = _branched_repo(tmp_path)
+ _git(repo, "merge", "--no-commit", "--no-ff", "main")
+ assert gate.resolve_base_point("main", cwd=repo) == base_tip
+
+
+def test_base_point_mid_merge_of_an_older_side_branch_keeps_the_newer_branch_point(tmp_path):
+ repo = _init_repo(tmp_path)
+ _commit(repo, "shared.txt")
+ _git(repo, "checkout", "-q", "-b", "old-side")
+ _commit(repo, "old.txt")
+ _git(repo, "checkout", "-q", "main")
+ newer_point = _commit(repo, "drift.txt")
+ _git(repo, "checkout", "-q", "-b", "feature")
+ _commit(repo, "feature.txt")
+ _git(repo, "merge", "--no-commit", "--no-ff", "old-side")
+ assert gate.resolve_base_point("main", cwd=repo) == newer_point
diff --git a/tests/test_litellm/test_type_discipline_gate.py b/tests/test_litellm/test_type_discipline_gate.py
index 688174b68ca..1832668e7c3 100644
--- a/tests/test_litellm/test_type_discipline_gate.py
+++ b/tests/test_litellm/test_type_discipline_gate.py
@@ -6,6 +6,7 @@ drift-safe breach check). Both are pinned here.
"""
import importlib.util
+import subprocess
from pathlib import Path
_MODULE_PATH = Path(__file__).resolve().parents[2] / "scripts" / "type_discipline_gate.py"
@@ -63,3 +64,43 @@ def test_update_leaves_rules_seeded_on_this_branch_untouched():
"LIT001": {"limit": 85},
"LIT010": {"limit": 24600},
}
+
+
+def _git(cwd, *args):
+ proc = subprocess.run(["git", *args], cwd=cwd, capture_output=True, text=True)
+ assert proc.returncode == 0, proc.stderr
+ return proc.stdout.strip()
+
+
+def _commit(cwd, name):
+ (cwd / name).write_text(name)
+ _git(cwd, "add", "-A")
+ _git(cwd, "commit", "-q", "-m", name)
+ return _git(cwd, "rev-parse", "HEAD")
+
+
+def _branched_repo(tmp_path):
+ repo = tmp_path / "repo"
+ repo.mkdir()
+ _git(repo, "init", "-q", "-b", "main")
+ _git(repo, "config", "user.email", "[email protected]")
+ _git(repo, "config", "user.name", "gate")
+ _git(repo, "config", "commit.gpgsign", "false")
+ branch_point = _commit(repo, "shared.txt")
+ _git(repo, "checkout", "-q", "-b", "feature")
+ _commit(repo, "feature.txt")
+ _git(repo, "checkout", "-q", "main")
+ base_tip = _commit(repo, "drift.txt")
+ _git(repo, "checkout", "-q", "feature")
+ return repo, branch_point, base_tip
+
+
+def test_base_point_is_the_branch_point_when_no_merge_is_in_progress(tmp_path):
+ repo, branch_point, _ = _branched_repo(tmp_path)
+ assert gate.resolve_base_point("main", cwd=repo) == branch_point
+
+
+def test_base_point_mid_merge_advances_to_the_merged_in_base_tip(tmp_path):
+ repo, _, base_tip = _branched_repo(tmp_path)
+ _git(repo, "merge", "--no-commit", "--no-ff", "main")
+ assert gate.resolve_base_point("main", cwd=repo) == base_tip