File size: 7,123 Bytes
e000265
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f13d613
 
 
 
 
 
 
e000265
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f13d613
 
 
 
 
 
 
 
 
 
 
 
 
e000265
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f13d613
 
 
 
 
 
 
 
 
 
 
 
 
 
e000265
 
 
 
 
 
 
 
f13d613
 
e000265
 
f13d613
e000265
 
f13d613
e000265
 
 
 
 
 
 
f13d613
e000265
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from __future__ import annotations

import re
from typing import Any

import yaml


class AntiHackingDetector:
    """Detects shortcut behaviors that can game CI-fix rewards."""

    STAGE_SKIP_PATTERNS = (
        r"\bif\s*:\s*false\b",
        r"\bwhen\s*:\s*never\b",
        r"\bon\s*:\s*\[\s*\]\b",
        r"\bon\s*:\s*{}",
        r"\ballow_failure\s*:\s*true\b",
    )

    FAKE_SUCCESS_PATTERNS = (
        r"echo\s+[\"']?tests\s+passed[\"']?",
        r"echo\s+[\"']?success[\"']?",
        r"\bexit\s+0\b",
        r"\btrue\b\s*#?\s*force",
    )

    HARDCODED_OUTPUT_PATTERNS = (
        r"\bstatus\s*:\s*success\b",
        r"\bresult\s*:\s*passed\b",
        r"\btests?_passed\s*=\s*true\b",
        r"\bforce_success\b",
    )

    BROKEN_COMMAND_PATTERNS = (
        r"\bnpm\s+tset\b",
        r"\bpyhton\b",
        r"\bpip\s+isntall\b",
        r"\bgo\s+tset\b",
    )

    def penalty_stage_skipping(self, config_text: str) -> float:
        hits = self._count_hits(config_text, self.STAGE_SKIP_PATTERNS)
        if hits == 0:
            return 0.0
        return -min(0.20 * hits, 0.70)

    def penalty_fake_success(self, config_text: str) -> float:
        hits = self._count_hits(config_text, self.FAKE_SUCCESS_PATTERNS)
        if hits == 0:
            return 0.0

        normalized = (config_text or "").lower()
        has_real_test_cmd = any(token in normalized for token in ("npm test", "pytest", "go test", "mvn test", "yarn test", "pnpm test"))
        base = 0.15 if has_real_test_cmd else 0.25
        return -min(base * hits, 0.70)

    def penalty_hardcoded_outputs(self, config_text: str) -> float:
        hits = self._count_hits(config_text, self.HARDCODED_OUTPUT_PATTERNS)
        if hits == 0:
            return 0.0
        return -min(0.18 * hits, 0.60)

    def penalty_invalid_config(self, config_text: str) -> float:
        if not (config_text or "").strip():
            return -0.30
        if not self._is_yaml_valid(config_text):
            return -0.35
        return 0.0

    def penalty_breaking_pipeline(self, previous_config: str, new_config: str) -> float:
        if not previous_config or not new_config:
            return 0.0

        penalty = 0.0

        previous_valid = self._is_yaml_valid(previous_config)
        new_valid = self._is_yaml_valid(new_config)
        if previous_valid and not new_valid:
            penalty -= 0.40

        previous_stages = self._extract_stage_names(previous_config)
        new_stages = self._extract_stage_names(new_config)
        missing_stages = previous_stages - new_stages
        if missing_stages:
            penalty -= min(0.15 * len(missing_stages), 0.45)

        previous_broken = self._count_hits(previous_config, self.BROKEN_COMMAND_PATTERNS)
        new_broken = self._count_hits(new_config, self.BROKEN_COMMAND_PATTERNS)
        if new_broken > previous_broken:
            penalty -= min(0.10 * (new_broken - previous_broken), 0.30)

        return max(-1.0, penalty)

    def penalty_excessive_edits(
        self,
        edit_count: int | dict[str, Any] | None = None,
        changed_files_count: int = 0,
        changed_lines_count: int = 0,
    ) -> float:
        if isinstance(edit_count, dict):
            changed_files_count = int(edit_count.get("changed_files_count", changed_files_count) or 0)
            changed_lines_count = int(edit_count.get("changed_lines_count", changed_lines_count) or 0)
        elif isinstance(edit_count, int):
            changed_lines_count = max(changed_lines_count, int(edit_count))

        penalty = 0.0

        if changed_files_count > 5:
            penalty -= 0.15
        if changed_files_count > 10:
            penalty -= 0.25

        if changed_lines_count > 120:
            penalty -= 0.15
        if changed_lines_count > 300:
            penalty -= 0.25

        return max(-0.80, penalty)

    def penalty_timeout_abuse(self, step_count: int) -> float:
        if step_count > 30:
            return -0.80
        if step_count > 20:
            return -0.50
        return 0.0

    def penalty_bruteforce_attempts(self, consecutive_edit_actions: int, failed_validations: int) -> float:
        penalty = 0.0
        if consecutive_edit_actions >= 6:
            penalty -= 0.25
        if consecutive_edit_actions >= 10:
            penalty -= 0.35

        if failed_validations >= 3:
            penalty -= 0.20
        if failed_validations >= 6:
            penalty -= 0.35

        return max(-0.80, penalty)

    def total_penalty(
        self,
        current_config: str = "",
        previous_config: str = "",
        edit_count: int | dict[str, Any] | None = None,
        changed_files_count: int = 0,
        changed_lines_count: int = 0,
        step_count: int = 0,
        consecutive_edit_actions: int = 0,
        failed_validations: int = 0,
    ) -> float:
        total = 0.0
        total += self.penalty_invalid_config(current_config)
        total += self.penalty_stage_skipping(current_config)
        total += self.penalty_fake_success(current_config)
        total += self.penalty_hardcoded_outputs(current_config)
        total += self.penalty_breaking_pipeline(previous_config, current_config)
        total += self.penalty_excessive_edits(
            edit_count=edit_count,
            changed_files_count=changed_files_count,
            changed_lines_count=changed_lines_count,
        )
        total += self.penalty_timeout_abuse(step_count)
        total += self.penalty_bruteforce_attempts(consecutive_edit_actions, failed_validations)

        return round(total, 4)

    def _count_hits(self, text: str, patterns: tuple[str, ...]) -> int:
        text = text or ""
        return sum(1 for pattern in patterns if re.search(pattern, text, flags=re.IGNORECASE))

    def _is_yaml_valid(self, config_text: str) -> bool:
        if not (config_text or "").strip():
            return False
        try:
            yaml.safe_load(config_text)
            return True
        except yaml.YAMLError:
            return False

    def _extract_stage_names(self, config_text: str) -> set[str]:
        try:
            parsed = yaml.safe_load(config_text)
        except yaml.YAMLError:
            return set()

        if parsed is None:
            return set()

        stages: set[str] = set()
        self._walk_for_stages(parsed, stages)
        return stages

    def _walk_for_stages(self, node: Any, stages: set[str]) -> None:
        if isinstance(node, dict):
            for key, value in node.items():
                key_name = str(key).lower()
                if key_name in {"stages", "jobs", "job"}:
                    if isinstance(value, dict):
                        for stage_name in value.keys():
                            stages.add(str(stage_name))
                    elif isinstance(value, list):
                        for stage_name in value:
                            stages.add(str(stage_name))
                self._walk_for_stages(value, stages)
        elif isinstance(node, list):
            for item in node:
                self._walk_for_stages(item, stages)