1010import math
1111from typing import Any , Dict , List , Mapping , Optional , Tuple
1212
13+ from ._rounding import round_half_even
14+
1315RISK_VERSION = "v10"
1416TOTAL_CAP = 25.0
1517CONTRIBUTION_CAP = 16.0
@@ -74,9 +76,9 @@ def _coverage(m: Mapping[str, Any]) -> Dict[str, float]:
7476 all_sample = max (0.0 , _finite (m .get ("recent_pr_sample" )))
7577 all_total = max (1.0 , min (_finite (m .get ("total_pr_count" )), 30.0 ))
7678 return {
77- "repo" : round (_clamp01 (fetched / public_repos ), 4 ),
78- "merged_pr" : round (_clamp01 (merged_sample / merged_total ), 4 ),
79- "all_pr" : round (_clamp01 (all_sample / all_total ), 4 ),
79+ "repo" : round_half_even (_clamp01 (fetched / public_repos ), 4 ),
80+ "merged_pr" : round_half_even (_clamp01 (merged_sample / merged_total ), 4 ),
81+ "all_pr" : round_half_even (_clamp01 (all_sample / all_total ), 4 ),
8082 }
8183
8284
@@ -111,8 +113,8 @@ def _candidate(
111113 "flag" : flag ,
112114 "family" : family ,
113115 "disposition" : "note" ,
114- "severity" : round (_clamp01 (severity ), 4 ),
115- "confidence" : round (_clamp01 (confidence ), 4 ),
116+ "severity" : round_half_even (_clamp01 (severity ), 4 ),
117+ "confidence" : round_half_even (_clamp01 (confidence ), 4 ),
116118 "penalty" : 0.0 ,
117119 "detail" : detail ,
118120 "evidence" : _evidence (m , observed , sample , threshold ),
@@ -135,15 +137,15 @@ def _apply_cap(candidates: List[Dict[str, Any]], family: str, cap: float) -> flo
135137 raw_total = sum (item ["raw" ] for item in active )
136138 if raw_total <= 0 :
137139 return 0.0
138- target = round (min (cap , raw_total ), 2 )
140+ target = round_half_even (min (cap , raw_total ), 2 )
139141 factor = target / raw_total
140142 assigned = 0.0
141143 for index , item in enumerate (active ):
142- penalty = round (max (0.0 , target - assigned ), 2 ) if index == len (active ) - 1 else round (item ["raw" ] * factor , 2 )
144+ penalty = round_half_even (max (0.0 , target - assigned ), 2 ) if index == len (active ) - 1 else round_half_even (item ["raw" ] * factor , 2 )
143145 item ["signal" ]["penalty" ] = penalty
144146 if penalty > 0 :
145147 item ["signal" ]["disposition" ] = "penalty"
146- assigned = round (assigned + penalty , 2 )
148+ assigned = round_half_even (assigned + penalty , 2 )
147149 return assigned
148150
149151
@@ -195,7 +197,7 @@ def assess_risk(m: Mapping[str, Any]) -> Tuple[Dict[str, Any], List[Dict[str, An
195197 trivial_raw = 8 * _clamp01 ((trivial_rate - TRIVIAL_RATE ) / TRIVIAL_TRANSITION ) * _sample_factor (merged_sample ) if merged_sample >= MIN_CONTRIBUTION_SAMPLE and trivial_rate > TRIVIAL_RATE else 0.0
196198 if merged_sample > 0 and external_trivial > 0 and external_trivial / merged_sample > 0.3 :
197199 detail = "外部热门仓库中的低实质 PR 比例偏高,但合并样本不足 20 个,仅作风险提示。" if merged_sample < MIN_CONTRIBUTION_SAMPLE else "外部热门仓库中的低实质 PR 比例达到量化阈值;仅统计他人仓库,不惩罚正常自提 PR。"
198- _add_contribution (m , candidates , "trivial_pr_farming" , trivial_raw , trivial_severity , detail , {"external_trivial_pr_count" : external_trivial , "merged_pr_sample" : merged_sample , "smoothed_rate" : round (trivial_rate , 4 )}, merged_sample , {"minimum_sample" : MIN_CONTRIBUTION_SAMPLE , "smoothed_rate" : TRIVIAL_RATE }, coverage_confidence )
200+ _add_contribution (m , candidates , "trivial_pr_farming" , trivial_raw , trivial_severity , detail , {"external_trivial_pr_count" : external_trivial , "merged_pr_sample" : merged_sample , "smoothed_rate" : round_half_even (trivial_rate , 4 )}, merged_sample , {"minimum_sample" : MIN_CONTRIBUTION_SAMPLE , "smoothed_rate" : TRIVIAL_RATE }, coverage_confidence )
199201
200202 rejection_measured = m .get ("maintainer_closed_unmerged_pr_count" ) is not None
201203 rejected = max (0.0 , _finite (m .get ("maintainer_closed_unmerged_pr_count" ))) if rejection_measured else 0.0
@@ -206,7 +208,7 @@ def assess_risk(m: Mapping[str, Any]) -> Tuple[Dict[str, Any], List[Dict[str, An
206208 rejection_raw = 4 * _clamp01 ((rejection_lower - REJECTION_WILSON_LOWER ) / REJECTION_WILSON_LOWER ) if rejection_measured and decided >= MIN_REJECTION_DECISIONS and rejection_lower >= REJECTION_WILSON_LOWER else 0.0
207209 if rejection_measured and decided > 0 and rejection_rate >= REJECTION_WILSON_LOWER :
208210 detail = "维护者关闭未合并的 PR 比例偏高,但样本量或 Wilson 95% 下界未达到扣分门槛,仅作风险提示。" if decided < MIN_REJECTION_DECISIONS or rejection_lower < REJECTION_WILSON_LOWER else "维护者关闭未合并的 PR 的 Wilson 95% 下界达到量化阈值。"
209- _add_contribution (m , candidates , "high_pr_rejection" , rejection_raw , rejection_severity , detail , {"maintainer_closed_unmerged_pr_count" : rejected , "merged_pr_count" : _finite (m .get ("merged_pr_count" )), "decided_pr_count" : decided , "wilson_lower" : round (rejection_lower , 4 )}, decided , {"minimum_decisions" : MIN_REJECTION_DECISIONS , "wilson_lower" : REJECTION_WILSON_LOWER }, coverage_confidence )
211+ _add_contribution (m , candidates , "high_pr_rejection" , rejection_raw , rejection_severity , detail , {"maintainer_closed_unmerged_pr_count" : rejected , "merged_pr_count" : _finite (m .get ("merged_pr_count" )), "decided_pr_count" : decided , "wilson_lower" : round_half_even (rejection_lower , 4 )}, decided , {"minimum_decisions" : MIN_REJECTION_DECISIONS , "wilson_lower" : REJECTION_WILSON_LOWER }, coverage_confidence )
210212
211213 flood_sample = max (0.0 , _finite (m .get ("recent_pr_sample" )))
212214 share = _clamp01 (_finite (m .get ("top_repo_pr_share" )))
@@ -230,7 +232,7 @@ def assess_risk(m: Mapping[str, Any]) -> Tuple[Dict[str, Any], List[Dict[str, An
230232 follow_raw = 4 * _clamp01 ((FOLLOW_RATIO - follower_ratio ) / FOLLOW_RATIO ) * _clamp01 ((following - FOLLOW_MINIMUM ) / 3000 ) if following >= FOLLOW_MINIMUM and follower_ratio < FOLLOW_RATIO else 0.0
231233 follow : Optional [Dict [str , Any ]] = None
232234 if follow_observed :
233- follow = _candidate (m , "follow_farming" , "social" , 0.0 , _clamp01 ((FOLLOW_RATIO - follower_ratio ) / FOLLOW_RATIO ), coverage_confidence , "关注数量与粉丝数量明显失衡;当前只有单次快照,没有关注增长时间序列,不能单独判定刷粉。" , {"following" : following , "followers" : followers , "follower_ratio" : round (follower_ratio , 4 )}, threshold = {"minimum_following" : FOLLOW_MINIMUM , "follower_ratio" : FOLLOW_RATIO })
235+ follow = _candidate (m , "follow_farming" , "social" , 0.0 , _clamp01 ((FOLLOW_RATIO - follower_ratio ) / FOLLOW_RATIO ), coverage_confidence , "关注数量与粉丝数量明显失衡;当前只有单次快照,没有关注增长时间序列,不能单独判定刷粉。" , {"following" : following , "followers" : followers , "follower_ratio" : round_half_even (follower_ratio , 4 )}, threshold = {"minimum_following" : FOLLOW_MINIMUM , "follower_ratio" : FOLLOW_RATIO })
234236 candidates .append (follow )
235237
236238 engagement = m .get ("top_repo_engagement_ratio" )
@@ -252,7 +254,7 @@ def assess_risk(m: Mapping[str, Any]) -> Tuple[Dict[str, Any], List[Dict[str, An
252254
253255 contribution_penalty = _apply_cap (candidates , "contribution" , CONTRIBUTION_CAP )
254256 social_penalty = _apply_cap (candidates , "social" , SOCIAL_CAP )
255- total_penalty = round (min (TOTAL_CAP , contribution_penalty + social_penalty ), 2 )
257+ total_penalty = round_half_even (min (TOTAL_CAP , contribution_penalty + social_penalty ), 2 )
256258 signals = [item ["signal" ] for item in candidates ]
257259 notes = []
258260 red_flags = []
@@ -264,13 +266,13 @@ def assess_risk(m: Mapping[str, Any]) -> Tuple[Dict[str, Any], List[Dict[str, An
264266 note ["penalty" ] = 0.0
265267 note ["disposition" ] = "note"
266268 notes .append (note )
267- risk_score = round (total_penalty / TOTAL_CAP * 100 , 2 )
269+ risk_score = round_half_even (total_penalty / TOTAL_CAP * 100 , 2 )
268270 level = "high" if risk_score >= HIGH_MINIMUM else "review" if risk_score >= REVIEW_MINIMUM else "none"
269271 assessment = {
270272 "version" : RISK_VERSION ,
271273 "risk_score" : risk_score ,
272274 "level" : level ,
273- "confidence" : round (_confidence (coverage ) * 100 , 2 ),
275+ "confidence" : round_half_even (_confidence (coverage ) * 100 , 2 ),
274276 "applied_penalty" : total_penalty ,
275277 "signals" : signals ,
276278 "coverage" : coverage ,
0 commit comments