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
29 changes: 29 additions & 0 deletions .github/codex_auto_merge_policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,35 @@
},
"max_changed_files": 30,
"max_changed_lines": 2000,
"approved_change_bundles": [
{
"name": "retire-duplicate-aiaudit-pr-reviewer",
"exact_changed_paths": [
".github/actionlint.yaml",
".github/codex_auto_merge_policy.json",
".github/workflows/codex_pr_review.yml",
"README.md",
"README.zh-CN.md",
"client/gateway_client.py",
"docs/ai_autonomy_architecture.md",
"prompts/pr_review.md",
"scripts/run_codex_pr_review.py",
"service/dual_review_primary.py",
"service/org_health.py",
"tests/test_codex_audit_service_complexity.py",
"tests/test_dual_review_primary.py",
"tests/test_org_health.py",
"tests/test_run_codex_pr_review.py",
"tests/test_single_pr_reviewer_contract.py"
],
"exact_deleted_paths": [
"prompts/pr_review.md",
"scripts/run_codex_pr_review.py",
"tests/test_run_codex_pr_review.py"
],
"max_changed_lines": 3000
}
],
"pr_review": {
"enabled": true,
"block_severities": [
Expand Down
60 changes: 58 additions & 2 deletions scripts/gate_codex_app_review_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import json
import re
from pathlib import Path
from pathlib import Path, PurePosixPath
from typing import Any

DEFAULT_POLICY_PATH = Path(".github/codex_auto_merge_policy.json")
Expand Down Expand Up @@ -70,16 +70,72 @@ def scan_diff(diff_text: str, path_patterns: list[re.Pattern[str]]) -> list[str]
return list(dict.fromkeys(violations))


def _safe_exact_paths(values: Any) -> set[str] | None:
if not isinstance(values, list) or not values:
return None
paths: set[str] = set()
for value in values:
if not isinstance(value, str) or value != value.strip():
return None
path = PurePosixPath(value)
if (
not value
or path.is_absolute()
or path.as_posix() != value
or ".." in path.parts
or "\\" in value
or any(character in value for character in "*?[]")
or value in paths
):
return None
paths.add(value)
return paths


def check_metadata(files: list[dict[str, Any]], policy: dict[str, Any]) -> list[str]:
issues: list[str] = []
approved_deleted_paths: set[str] = set()
max_files = policy.get("max_changed_files", 50)
max_lines = policy.get("max_changed_lines", 5000)
changed_paths = {
filename
for f in files
if isinstance((filename := f.get("filename")), str)
}
removed_paths = {
f["filename"]
for f in files
if isinstance(f.get("filename"), str)
and (f.get("status") or "").lower().strip() == "removed"
}
configured_bundles = policy.get("approved_change_bundles", [])
if isinstance(configured_bundles, list):
for bundle in configured_bundles:
if not isinstance(bundle, dict):
continue
exact_changed_paths = _safe_exact_paths(bundle.get("exact_changed_paths"))
exact_deleted_paths = _safe_exact_paths(bundle.get("exact_deleted_paths"))
bundle_max_lines = bundle.get("max_changed_lines")
if (
exact_changed_paths is None
or exact_deleted_paths is None
or not exact_deleted_paths.issubset(exact_changed_paths)
or type(bundle_max_lines) is not int
or bundle_max_lines < max_lines
or changed_paths != exact_changed_paths
or removed_paths != exact_deleted_paths
):
continue
approved_deleted_paths = exact_deleted_paths
max_lines = bundle_max_lines
break

total_added = sum(f.get("additions", 0) or 0 for f in files)
total_deleted = sum(f.get("deletions", 0) or 0 for f in files)
for f in files:
filename = f.get("filename", "?")
status = (f.get("status") or "").lower().strip()
if status == "removed":
if status == "removed" and filename not in approved_deleted_paths:
issues.append(f"**File deleted**: `{filename}` — verify intentional")
elif status == "renamed":
issues.append(f"**File renamed**: `{f.get('previous_filename', '?')}` → `{filename}`")
Expand Down
93 changes: 93 additions & 0 deletions tests/test_gate_codex_app_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,99 @@ def test_collect_static_gate_issues_aggregates_metadata_and_diff(self) -> None:
self.assertTrue(any("Hardcoded secret" in issue for issue in issues))
self.assertTrue(any("Too many lines" in issue for issue in issues))

def test_check_metadata_allows_only_an_exact_base_approved_change_bundle(self) -> None:
files = [
{
"filename": "scripts/retired.py",
"status": "removed",
"additions": 0,
"deletions": 150,
},
{
"filename": "service/caller.py",
"status": "modified",
"additions": 2_400,
"deletions": 0,
},
]
policy = {
"max_changed_files": 10,
"max_changed_lines": 2_000,
"approved_change_bundles": [
{
"exact_changed_paths": [
"scripts/retired.py",
"service/caller.py",
],
"exact_deleted_paths": ["scripts/retired.py"],
"max_changed_lines": 3_000,
}
],
}

issues = gate_codex_app_review_static.check_metadata(files, policy)

self.assertEqual(issues, [])

def test_check_metadata_does_not_expand_line_budget_for_an_unrelated_change(self) -> None:
files = [
{
"filename": "service/unrelated.py",
"status": "modified",
"additions": 2_500,
"deletions": 0,
},
]
policy = {
"max_changed_files": 10,
"max_changed_lines": 2_000,
"approved_change_bundles": [
{
"exact_changed_paths": [
"scripts/retired.py",
"service/caller.py",
],
"exact_deleted_paths": ["scripts/retired.py"],
"max_changed_lines": 3_000,
}
],
}

issues = gate_codex_app_review_static.check_metadata(files, policy)

self.assertTrue(any("Too many lines" in issue for issue in issues))

def test_check_metadata_rejects_partial_or_unsafe_change_bundles(self) -> None:
files = [
{
"filename": "scripts/retired.py",
"status": "removed",
"additions": 0,
"deletions": 10,
},
]
for exact_changed_paths in (
["scripts/retired.py", "service/caller.py"],
["scripts/*.py"],
["../scripts/retired.py"],
["/scripts/retired.py"],
["scripts\\retired.py"],
):
with self.subTest(exact_changed_paths=exact_changed_paths):
policy = {
"max_changed_files": 10,
"max_changed_lines": 100,
"approved_change_bundles": [
{
"exact_changed_paths": exact_changed_paths,
"exact_deleted_paths": ["scripts/retired.py"],
"max_changed_lines": 200,
}
],
}
issues = gate_codex_app_review_static.check_metadata(files, policy)
self.assertTrue(any("scripts/retired.py" in issue for issue in issues))


if __name__ == "__main__":
unittest.main()
Loading