pr-proof 0.0.0-stage → 0.1.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/Procfile +2 -0
- package/README.md +300 -2
- package/app/__init__.py +1 -0
- package/app/api/__init__.py +1 -0
- package/app/api/cli.py +35 -0
- package/app/api/github.py +168 -0
- package/app/api/history.py +123 -0
- package/app/api/main.py +95 -0
- package/app/api/queue.py +45 -0
- package/app/api/responses.py +28 -0
- package/app/api/signature.py +19 -0
- package/app/api/sync.py +189 -0
- package/app/api/sync_rules_pure.py +71 -0
- package/app/api/webhook.py +166 -0
- package/app/setup/__init__.py +4 -0
- package/app/setup/cli.py +90 -0
- package/app/setup/configure.py +720 -0
- package/app/setup/files.py +93 -0
- package/app/setup/github_setup.py +149 -0
- package/app/setup/install_hints.json +32 -0
- package/app/setup/setup_pure.py +312 -0
- package/app/setup/start.py +113 -0
- package/app/shared/__init__.py +1 -0
- package/app/shared/comment_pure.py +29 -0
- package/app/shared/config.py +106 -0
- package/app/shared/db_pure.py +339 -0
- package/app/shared/embed_models_pure.py +55 -0
- package/app/shared/feedback_pure.py +84 -0
- package/app/shared/github_auth.py +74 -0
- package/app/shared/job_pure.py +43 -0
- package/app/shared/notify.py +97 -0
- package/app/shared/ops_alert_pure.py +38 -0
- package/app/shared/state_keys_pure.py +24 -0
- package/app/shared/version.py +27 -0
- package/app/worker/__init__.py +1 -0
- package/app/worker/access_pure.py +180 -0
- package/app/worker/ai/__init__.py +1 -0
- package/app/worker/ai/claude_cli.py +684 -0
- package/app/worker/ai/mcp_relay.py +42 -0
- package/app/worker/ai/path_hook.py +103 -0
- package/app/worker/ai/tool_server.py +139 -0
- package/app/worker/ai/workdir.py +333 -0
- package/app/worker/base_config.py +173 -0
- package/app/worker/budget.py +100 -0
- package/app/worker/cache_key_pure.py +34 -0
- package/app/worker/call_budget_pure.py +132 -0
- package/app/worker/checker_authoring_pure.py +599 -0
- package/app/worker/checker_pure.py +196 -0
- package/app/worker/cli.py +97 -0
- package/app/worker/command.py +191 -0
- package/app/worker/command_pure.py +38 -0
- package/app/worker/coverage.py +123 -0
- package/app/worker/coverage_pure.py +202 -0
- package/app/worker/deps.py +22 -0
- package/app/worker/diff_pure.py +419 -0
- package/app/worker/doc_rules_pure.py +201 -0
- package/app/worker/doc_rules_report.py +979 -0
- package/app/worker/eval_run_pure.py +241 -0
- package/app/worker/evaluation.py +151 -0
- package/app/worker/feedback.py +448 -0
- package/app/worker/feedback_pure.py +131 -0
- package/app/worker/feedback_report_pure.py +90 -0
- package/app/worker/feedback_triage.py +85 -0
- package/app/worker/feedback_triage_pure.py +363 -0
- package/app/worker/fix/__init__.py +1 -0
- package/app/worker/fix/decide.py +197 -0
- package/app/worker/fix/feedback_fix_pure.py +125 -0
- package/app/worker/fix/fix_decision_pure.py +386 -0
- package/app/worker/fix/fix_pure.py +70 -0
- package/app/worker/fix/mechanical_fix.py +214 -0
- package/app/worker/fix/model_fix.py +575 -0
- package/app/worker/fix/model_fix_pure.py +593 -0
- package/app/worker/fix/proposal.py +584 -0
- package/app/worker/fix/proposal_pure.py +103 -0
- package/app/worker/fix/scope.py +40 -0
- package/app/worker/fix/scope_pure.py +222 -0
- package/app/worker/generated_tests_pure.py +442 -0
- package/app/worker/github.py +471 -0
- package/app/worker/graph.py +963 -0
- package/app/worker/guard.py +91 -0
- package/app/worker/lang/__init__.py +0 -0
- package/app/worker/lang/install_pure.py +25 -0
- package/app/worker/lang/python.py +862 -0
- package/app/worker/loop.py +200 -0
- package/app/worker/main.py +76 -0
- package/app/worker/markdown_pure.py +29 -0
- package/app/worker/mcp_pure.py +150 -0
- package/app/worker/memory.py +503 -0
- package/app/worker/merged_fix_pure.py +236 -0
- package/app/worker/pr_check_report_pure.py +201 -0
- package/app/worker/pr_description_report_pure.py +112 -0
- package/app/worker/preflight.py +125 -0
- package/app/worker/rag_pure.py +285 -0
- package/app/worker/rag_switch_pure.py +203 -0
- package/app/worker/records.py +207 -0
- package/app/worker/records_pure.py +239 -0
- package/app/worker/repo.py +588 -0
- package/app/worker/repo_config_pure.py +269 -0
- package/app/worker/report.py +300 -0
- package/app/worker/retry.py +65 -0
- package/app/worker/rule_pure.py +223 -0
- package/app/worker/rule_reading_pure.py +503 -0
- package/app/worker/rules/__init__.py +1 -0
- package/app/worker/rules/checkers.py +615 -0
- package/app/worker/rules/detect.py +154 -0
- package/app/worker/rules/doc_rules.py +301 -0
- package/app/worker/rules/doc_sections_pure.py +168 -0
- package/app/worker/rules/judge.py +410 -0
- package/app/worker/rules/judge_pure.py +627 -0
- package/app/worker/rules/pr_check.py +404 -0
- package/app/worker/rules/pr_check_pure.py +922 -0
- package/app/worker/rules/pr_description.py +218 -0
- package/app/worker/rules/pr_description_pure.py +743 -0
- package/app/worker/rules/rules.py +314 -0
- package/app/worker/rules/run_check.py +95 -0
- package/app/worker/rules/run_check_pure.py +219 -0
- package/app/worker/rules/tool_choice_pure.py +135 -0
- package/app/worker/sandbox/__init__.py +1 -0
- package/app/worker/sandbox/checker_runner.py +412 -0
- package/app/worker/sandbox/container.py +130 -0
- package/app/worker/sandbox/docker_cli.py +170 -0
- package/app/worker/sandbox/safe_read.py +32 -0
- package/app/worker/sandbox/toolcache.py +425 -0
- package/app/worker/state.py +602 -0
- package/app/worker/test_lines.py +77 -0
- package/app/worker/test_run_pure.py +133 -0
- package/app/worker/text_edit_pure.py +66 -0
- package/app/worker/tools_pure.py +152 -0
- package/app/worker/update.py +305 -0
- package/app/worker/violation_pure.py +33 -0
- package/bin/pr-proof.js +173 -0
- package/docker/sandbox/Dockerfile +12 -0
- package/docker/sandbox/coveragerc +19 -0
- package/docker-compose.yml +5 -0
- package/package.json +31 -4
- package/pyproject.toml +60 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"""수정안 PR 규칙 (docs/DESIGN.md 4.8, 4.9): 브랜치 이름, 푸시 조건(상한·비밀값). 판단만 한다."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from collections.abc import Iterable, Sequence
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from typing import Final
|
|
7
|
+
|
|
8
|
+
from app.worker.cache_key_pure import text_hash
|
|
9
|
+
|
|
10
|
+
BRANCH_PREFIX: Final = "pipeline/"
|
|
11
|
+
MAX_FILES: Final = 20
|
|
12
|
+
MAX_CHANGED_LINES: Final = 500
|
|
13
|
+
PROPOSAL_MARKER: Final = "<!-- pr-proof:proposal -->"
|
|
14
|
+
|
|
15
|
+
# 토큰·키로 보이는 패턴. 파이프라인이 가진 비밀값(웹훅 시크릿)은 따로 그대로 찾는다
|
|
16
|
+
SECRET_PATTERNS: Final = (
|
|
17
|
+
re.compile(r"gh[pousr]_[A-Za-z0-9]{36,}"),
|
|
18
|
+
re.compile(r"github_pat_[A-Za-z0-9_]{40,}"),
|
|
19
|
+
re.compile(r"xox[abpors]-[A-Za-z0-9-]{10,}"),
|
|
20
|
+
re.compile(r"hooks\.slack\.com/services/[A-Za-z0-9/]+"),
|
|
21
|
+
re.compile(r"AKIA[0-9A-Z]{16}"),
|
|
22
|
+
re.compile(r"-----BEGIN [A-Z ]*PRIVATE KEY-----"),
|
|
23
|
+
re.compile(r"sk-ant-[A-Za-z0-9_-]{20,}"),
|
|
24
|
+
)
|
|
25
|
+
SECRET_BLOCKED: Final = "비밀값으로 보이는 내용이 있음"
|
|
26
|
+
MIN_LITERAL_SECRET: Final = 8 # 너무 짧은 값은 흔한 문자열과 겹친다
|
|
27
|
+
# 위반 범위(블록 전체)를 다시 쓰는 규칙. PR이 블록의 한 줄만 바꿔도 기존 줄 순서가 바뀔 수 있다
|
|
28
|
+
BLOCK_REWRITE_CODES: Final = frozenset({"I001"})
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def branch_name(pr_number: int, skill: str) -> str:
|
|
32
|
+
return f"{BRANCH_PREFIX}pr-{pr_number}-fix-{skill}"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def review_marker(patch: str) -> str:
|
|
36
|
+
"""수정안 PR의 줄 리뷰에 넣는 표시 (5.6). 같은 패치의 리뷰가 이미 있으면 다시 달지 않는다."""
|
|
37
|
+
return f"<!-- pr-proof:review {text_hash(patch)[:16]} -->"
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _added_lines(patch: str) -> list[str]:
|
|
41
|
+
return [
|
|
42
|
+
line[1:]
|
|
43
|
+
for line in patch.splitlines()
|
|
44
|
+
if line.startswith("+") and not line.startswith("+++")
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def changed_line_count(patch: str) -> int:
|
|
49
|
+
return sum(
|
|
50
|
+
1
|
|
51
|
+
for line in patch.splitlines()
|
|
52
|
+
if (line.startswith("+") and not line.startswith("+++"))
|
|
53
|
+
or (line.startswith("-") and not line.startswith("---"))
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def find_secret(patch: str, literals: Iterable[str] = ()) -> str | None:
|
|
58
|
+
"""추가된 줄에 비밀값으로 보이는 것이 있으면 그 종류.
|
|
59
|
+
|
|
60
|
+
값 자체는 돌려주지 않는다 (로그와 코멘트에 남지 않게).
|
|
61
|
+
"""
|
|
62
|
+
return find_secret_in_text("\n".join(_added_lines(patch)), literals)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def find_secret_in_text(text: str, literals: Iterable[str] = ()) -> str | None:
|
|
66
|
+
"""글(모델이 쓴 설명 등) 전체에서 비밀값으로 보이는 것의 종류. 값은 돌려주지 않는다."""
|
|
67
|
+
for literal in literals:
|
|
68
|
+
if len(literal) >= MIN_LITERAL_SECRET and literal in text:
|
|
69
|
+
return "파이프라인 비밀값"
|
|
70
|
+
for pattern in SECRET_PATTERNS:
|
|
71
|
+
if pattern.search(text):
|
|
72
|
+
return "토큰·키 형식"
|
|
73
|
+
return None
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def push_blocker(patch: str, files: Sequence[str], literals: Iterable[str] = ()) -> str | None:
|
|
77
|
+
"""제안 PR을 올리면 안 되는 이유. 올려도 되면 None."""
|
|
78
|
+
if not files or not patch.strip():
|
|
79
|
+
return "바뀐 내용이 없음"
|
|
80
|
+
if len(files) > MAX_FILES:
|
|
81
|
+
return f"바뀐 파일이 {len(files)}개로 상한({MAX_FILES}개)을 넘음"
|
|
82
|
+
lines = changed_line_count(patch)
|
|
83
|
+
if lines > MAX_CHANGED_LINES:
|
|
84
|
+
return f"바뀐 줄이 {lines}줄로 상한({MAX_CHANGED_LINES}줄)을 넘음"
|
|
85
|
+
kind = find_secret(patch, literals)
|
|
86
|
+
if kind is not None:
|
|
87
|
+
return f"{SECRET_BLOCKED} ({kind})"
|
|
88
|
+
return None
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
@dataclass(frozen=True)
|
|
92
|
+
class ProposalResult:
|
|
93
|
+
"""제안 PR을 열었으면 number, 못 열었으면 reason."""
|
|
94
|
+
|
|
95
|
+
number: int | None = None
|
|
96
|
+
url: str = ""
|
|
97
|
+
reason: str = ""
|
|
98
|
+
updated: bool = False # 같은 브랜치의 열린 제안을 새 커밋으로 바꿈
|
|
99
|
+
|
|
100
|
+
@property
|
|
101
|
+
def secret_blocked(self) -> bool:
|
|
102
|
+
"""비밀값 때문에 막았다. 수정 내용도 코멘트에 싣지 않는다."""
|
|
103
|
+
return self.reason.startswith(SECRET_BLOCKED)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""AI 수정의 허용 범위 검사 (docs/DESIGN.md F7 범위 검사 방법·무시된 파일).
|
|
2
|
+
|
|
3
|
+
git은 호스트에서 실행한다. 판정은 `app/worker/fix/scope_pure.py`가 한다.
|
|
4
|
+
|
|
5
|
+
순서: AI를 처음 실행하기 직전에 `snapshot_ignored`로 스냅샷을 남기고, AI 실행이 끝날 때마다
|
|
6
|
+
샌드박스에서 코드를 실행하기 전에 `check_scope`로 확인한다. 재시도 사이에 스냅샷을 다시 찍지 않는다
|
|
7
|
+
(이전 회차에 AI가 만든 무시된 파일이 '이미 있던 파일'이 되지 않게).
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
from app.worker import repo
|
|
13
|
+
from app.worker.fix.scope_pure import (
|
|
14
|
+
AI_POLICY,
|
|
15
|
+
ScopePolicy,
|
|
16
|
+
ScopeResult,
|
|
17
|
+
judge_scope,
|
|
18
|
+
parse_ignored,
|
|
19
|
+
parse_porcelain,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def snapshot_ignored(worktree: Path) -> frozenset[str]:
|
|
24
|
+
"""지금 있는 무시된 파일 (리포 기준 경로). 파이프라인 산출물은 뺀다."""
|
|
25
|
+
return parse_ignored(repo.ignored_status(worktree))
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def check_scope(
|
|
29
|
+
worktree: Path,
|
|
30
|
+
allowed: set[str],
|
|
31
|
+
ignored_before: frozenset[str],
|
|
32
|
+
*,
|
|
33
|
+
policy: ScopePolicy = AI_POLICY,
|
|
34
|
+
) -> ScopeResult:
|
|
35
|
+
"""worktree의 모든 변경과 새로 생긴 무시된 파일이 허용 범위 안인지. git 실패는 GitError."""
|
|
36
|
+
entries = parse_porcelain(repo.status(worktree))
|
|
37
|
+
# 지금 심볼릭 링크인 바뀐 경로. 삭제된 경로는 없으므로 False다
|
|
38
|
+
symlinks = frozenset(e.path for e in entries if (worktree / e.path).is_symlink())
|
|
39
|
+
new_ignored = snapshot_ignored(worktree) - ignored_before
|
|
40
|
+
return judge_scope(entries, allowed, policy, new_ignored=new_ignored, symlinks=symlinks)
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
"""수정안의 허용 변경 범위 판정 (docs/DESIGN.md F7 허용 변경 범위·범위 검사 방법·무시된 파일).
|
|
2
|
+
|
|
3
|
+
`git status --porcelain=v1 -z` 출력을 읽어 판정만 한다. git 실행은 `app/worker/fix/scope.py`가 한다.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import re
|
|
7
|
+
from collections.abc import Iterable
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from pathlib import PurePosixPath
|
|
10
|
+
from typing import Final
|
|
11
|
+
|
|
12
|
+
# 범위 검사에서 빼는 파이프라인 산출물. `.venv`는 샌드박스의 .venv 마운트 지점이다
|
|
13
|
+
# (`app/worker/sandbox/container.py`의 OUT_DIR, `app/worker/lang/python.py`의 VENV_IN_CONTAINER와
|
|
14
|
+
# 맞춘다)
|
|
15
|
+
PIPELINE_OUTPUTS: Final = (".pipeline-out/", ".venv/")
|
|
16
|
+
_PIPELINE_OUTPUT_DIRS: Final = (".pipeline-out", ".venv")
|
|
17
|
+
|
|
18
|
+
# 테스트 파일: 이 이름의 폴더 아래 모든 파일, 또는 이 패턴의 파일 이름
|
|
19
|
+
TEST_DIRS: Final = frozenset({"tests", "test"})
|
|
20
|
+
TEST_FILE_NAMES: Final = frozenset({"conftest.py"})
|
|
21
|
+
GITHUB_DIR: Final = ".github/"
|
|
22
|
+
# 수정 작성의 모델이 새로 만드는 테스트 파일 (4.7). 저장소의 테스트 폴더 바로 아래, 이름으로
|
|
23
|
+
# 파이프라인이 만든 것임이 보이는 새 파일만이다. 있던 테스트 파일, conftest.py, 테스트 설정은 밖이다
|
|
24
|
+
GENERATED_TEST_PREFIX: Final = "test_pipeline_"
|
|
25
|
+
_GENERATED_NAME: Final = re.compile(r"test_pipeline_[a-z0-9_]{1,40}\.py")
|
|
26
|
+
# 새 파일로 보이는 porcelain 상태: 추적하지 않는 새 파일, 그리고 diff에 보이려고 `git add -N`한 파일
|
|
27
|
+
_NEW_FILE_CODES: Final = frozenset({"??", " A"})
|
|
28
|
+
|
|
29
|
+
# porcelain v1의 병합 충돌 상태 (XY)
|
|
30
|
+
_UNMERGED: Final = frozenset({"DD", "AU", "UD", "UA", "DU", "AA", "UU"})
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass(frozen=True)
|
|
34
|
+
class StatusEntry:
|
|
35
|
+
"""`git status --porcelain=v1 -z` 한 줄. 이름 변경·복사면 orig_path가 있다."""
|
|
36
|
+
|
|
37
|
+
code: str # XY 두 글자. 예: " M", "??", "R ", "!!"
|
|
38
|
+
path: str
|
|
39
|
+
orig_path: str | None = None
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def parse_porcelain(output: str) -> list[StatusEntry]:
|
|
43
|
+
"""`git status --porcelain=v1 -z` 출력. 이름 변경·복사는 다음 항목이 옛 경로다.
|
|
44
|
+
|
|
45
|
+
-z 형식은 경로를 따옴표나 이스케이프 없이 그대로 내므로 공백·유니코드·줄바꿈이 든 경로도 읽는다.
|
|
46
|
+
"""
|
|
47
|
+
parts = output.split("\0")
|
|
48
|
+
entries: list[StatusEntry] = []
|
|
49
|
+
i = 0
|
|
50
|
+
while i < len(parts):
|
|
51
|
+
item = parts[i]
|
|
52
|
+
i += 1
|
|
53
|
+
if len(item) < 4:
|
|
54
|
+
continue
|
|
55
|
+
code, path = item[:2], item[3:]
|
|
56
|
+
orig = None
|
|
57
|
+
if code[0] in "RC" or code[1] in "RC":
|
|
58
|
+
orig = parts[i] if i < len(parts) else None
|
|
59
|
+
i += 1
|
|
60
|
+
entries.append(StatusEntry(code=code, path=path, orig_path=orig))
|
|
61
|
+
return entries
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def parse_ignored(output: str) -> frozenset[str]:
|
|
65
|
+
"""`--ignored` 출력에서 무시된 파일(`!!`)만. 파이프라인 산출물은 뺀다."""
|
|
66
|
+
return frozenset(
|
|
67
|
+
e.path for e in parse_porcelain(output) if e.code == "!!" and not is_pipeline_output(e.path)
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def is_pipeline_output(path: str) -> bool:
|
|
72
|
+
return path.startswith(PIPELINE_OUTPUTS) or path in _PIPELINE_OUTPUT_DIRS
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def is_test_file(path: str) -> bool:
|
|
76
|
+
"""테스트 파일인지. 경로만 보고 정한다 (리포 설정의 testpaths 등은 보지 않는다).
|
|
77
|
+
|
|
78
|
+
`tests/`·`test/` 폴더 아래의 모든 파일(데이터·픽스처 포함, 어느 깊이든),
|
|
79
|
+
`test_*.py`, `*_test.py`, `conftest.py`.
|
|
80
|
+
"""
|
|
81
|
+
p = PurePosixPath(path.rstrip("/"))
|
|
82
|
+
if any(part in TEST_DIRS for part in p.parts[:-1]):
|
|
83
|
+
return True
|
|
84
|
+
name = p.name
|
|
85
|
+
return (
|
|
86
|
+
name in TEST_FILE_NAMES
|
|
87
|
+
or (name.startswith("test_") and name.endswith(".py"))
|
|
88
|
+
or name.endswith("_test.py")
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def generated_test_dir(dirs: Iterable[str]) -> str:
|
|
93
|
+
"""새 테스트 파일을 둘 폴더. dirs는 저장소 맨 위의 진짜 폴더 이름들이다. `tests`가 있으면
|
|
94
|
+
그것, 없고 `test`가 있으면 그것, 둘 다 없으면 `tests`."""
|
|
95
|
+
names = set(dirs)
|
|
96
|
+
return "test" if "tests" not in names and "test" in names else "tests"
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def is_generated_test(path: str, test_dir: str) -> bool:
|
|
100
|
+
"""test_dir 바로 아래의 `test_pipeline_<이름>.py`인지. 이름은 영문 소문자·숫자·밑줄 40자까지."""
|
|
101
|
+
folder, _, name = path.rpartition("/")
|
|
102
|
+
return folder == test_dir and bool(_GENERATED_NAME.fullmatch(name))
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@dataclass(frozen=True)
|
|
106
|
+
class ScopePolicy:
|
|
107
|
+
"""수정 방식마다 다른 허용 규칙."""
|
|
108
|
+
|
|
109
|
+
modify_only: bool # 허용 파일의 수정(M)만 허락. 새 파일·삭제·이름 변경은 모두 밖이다
|
|
110
|
+
protect_tests: bool # 허용 목록에 있어도 테스트 파일 변경은 밖이다
|
|
111
|
+
protect_github: bool # 허용 목록에 있어도 `.github/` 변경은 밖이다
|
|
112
|
+
# 테스트 파일이지만 바꿔도 되는 경로. 코드 이동에 따른 import 경로 변경용 (4단계 structure).
|
|
113
|
+
# 호출하는 쪽이 바뀐 내용이 import 줄뿐인지 따로 확인해야 한다. 기본은 비어 있다
|
|
114
|
+
test_files_allowed: frozenset[str] = frozenset()
|
|
115
|
+
# 새로 만들어도 되는 테스트 파일의 폴더 (`is_generated_test`). None이면 만들 수 없다
|
|
116
|
+
generated_test_dir: str | None = None
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
# 기계적 수정: 위반이 있는 파일의 내용만 바꾼다. 파일을 만들거나 지울 이유가 없다.
|
|
120
|
+
# 테스트 파일의 위반도 고치므로 테스트 파일을 막지 않는다 (결과는 테스트 비교로 검증, 4.8의 1번)
|
|
121
|
+
MECHANICAL_POLICY: Final = ScopePolicy(modify_only=True, protect_tests=False, protect_github=False)
|
|
122
|
+
# 모델 수정: 허용 파일 안에서 새 파일·삭제·이름 변경까지 허락한다
|
|
123
|
+
AI_POLICY: Final = ScopePolicy(modify_only=False, protect_tests=True, protect_github=True)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
@dataclass(frozen=True)
|
|
127
|
+
class ScopeViolation:
|
|
128
|
+
code: str # porcelain XY를 공백 없이. 예: "M", "??", "R", "!!"
|
|
129
|
+
path: str
|
|
130
|
+
reason: str
|
|
131
|
+
|
|
132
|
+
@property
|
|
133
|
+
def label(self) -> str:
|
|
134
|
+
return f"{self.code} {self.path}"
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
@dataclass(frozen=True)
|
|
138
|
+
class ScopeResult:
|
|
139
|
+
violations: tuple[ScopeViolation, ...] = ()
|
|
140
|
+
|
|
141
|
+
@property
|
|
142
|
+
def ok(self) -> bool:
|
|
143
|
+
return not self.violations
|
|
144
|
+
|
|
145
|
+
@property
|
|
146
|
+
def detail(self) -> str:
|
|
147
|
+
"""코멘트에 넣는 한 줄. 경로마다 이유를 붙인다."""
|
|
148
|
+
return ", ".join(f"{v.label} ({v.reason})" for v in self.violations)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def _new_generated_test(e: StatusEntry, policy: ScopePolicy) -> bool:
|
|
152
|
+
"""정한 자리에 새로 만든 테스트 파일. 이름 변경·복사의 결과는 새 파일로 보지 않는다."""
|
|
153
|
+
return (
|
|
154
|
+
policy.generated_test_dir is not None
|
|
155
|
+
and e.orig_path is None
|
|
156
|
+
and e.code in _NEW_FILE_CODES
|
|
157
|
+
and is_generated_test(e.path, policy.generated_test_dir)
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _path_reason(path: str, allowed: set[str], policy: ScopePolicy) -> str | None:
|
|
162
|
+
if policy.protect_github and path.startswith(GITHUB_DIR):
|
|
163
|
+
return "`.github/` 변경"
|
|
164
|
+
if policy.protect_tests and is_test_file(path) and path not in policy.test_files_allowed:
|
|
165
|
+
return "테스트 파일 변경"
|
|
166
|
+
if path not in allowed:
|
|
167
|
+
return "허용 목록 밖"
|
|
168
|
+
return None
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def _entry_reason(e: StatusEntry, symlinks: frozenset[str]) -> str | None:
|
|
172
|
+
"""경로와 무관하게 버리는 변경."""
|
|
173
|
+
if e.code in _UNMERGED:
|
|
174
|
+
return "병합 충돌 상태"
|
|
175
|
+
if "T" in e.code:
|
|
176
|
+
return "파일 종류 변경" # 일반 파일 ↔ 심볼릭 링크·서브모듈
|
|
177
|
+
if e.path in symlinks:
|
|
178
|
+
return "심볼릭 링크"
|
|
179
|
+
return None
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def judge_scope(
|
|
183
|
+
entries: Iterable[StatusEntry],
|
|
184
|
+
allowed: Iterable[str],
|
|
185
|
+
policy: ScopePolicy,
|
|
186
|
+
*,
|
|
187
|
+
new_ignored: Iterable[str] = (),
|
|
188
|
+
symlinks: frozenset[str] = frozenset(),
|
|
189
|
+
) -> ScopeResult:
|
|
190
|
+
"""허용 범위 밖의 변경을 모두 모은다. 하나라도 있으면 수정안 전체를 버린다.
|
|
191
|
+
|
|
192
|
+
- 이름 변경·복사는 옛 경로와 새 경로가 둘 다 허용 범위 안이어야 한다.
|
|
193
|
+
- symlinks: 바뀐 경로 중 지금 심볼릭 링크인 것 (worker가 확인해 넘긴다). 링크는 worktree 밖을
|
|
194
|
+
가리킬 수 있어 허용 파일이라도 버린다.
|
|
195
|
+
- new_ignored: AI 실행 직전 스냅샷에 없던 무시된 파일. 경로와 무관하게 버린다.
|
|
196
|
+
- 경로가 모두 파이프라인 산출물인 변경은 보지 않는다.
|
|
197
|
+
- policy.generated_test_dir가 있으면 그 폴더에 정한 이름으로 새로 만든 테스트 파일은 허용 목록
|
|
198
|
+
밖이어도 받는다. 같은 이름이라도 있던 파일의 수정·삭제는 테스트 파일 변경이다.
|
|
199
|
+
"""
|
|
200
|
+
allowed_set = set(allowed)
|
|
201
|
+
bad: list[ScopeViolation] = []
|
|
202
|
+
for e in entries:
|
|
203
|
+
paths = [e.path] + ([e.orig_path] if e.orig_path else [])
|
|
204
|
+
if all(is_pipeline_output(p) for p in paths):
|
|
205
|
+
continue
|
|
206
|
+
code = e.code.replace(" ", "")
|
|
207
|
+
if policy.modify_only:
|
|
208
|
+
if set(code) != {"M"}:
|
|
209
|
+
bad.append(ScopeViolation(code, e.path, "수정 외 변경"))
|
|
210
|
+
continue
|
|
211
|
+
elif reason := _entry_reason(e, symlinks):
|
|
212
|
+
bad.append(ScopeViolation(code, e.path, reason))
|
|
213
|
+
continue
|
|
214
|
+
if _new_generated_test(e, policy):
|
|
215
|
+
continue
|
|
216
|
+
for p in paths:
|
|
217
|
+
if reason := _path_reason(p, allowed_set, policy):
|
|
218
|
+
bad.append(ScopeViolation(code, p, reason))
|
|
219
|
+
for p in sorted(new_ignored):
|
|
220
|
+
if not is_pipeline_output(p):
|
|
221
|
+
bad.append(ScopeViolation("!!", p, "새로 생긴 무시된 파일"))
|
|
222
|
+
return ScopeResult(tuple(bad))
|