Merged
Conversation
There was a problem hiding this comment.
Auto Review (Round 1) — Changes Requested
第 1 轮审查:发现 1 个 Critical 问题(O(n²) 性能风险可能导致 GitHub Actions 超时)和 2 个 Warning(异常处理不完整、多 JSON 对象时的边界行为)。核心思路正确(逐位置解析避免贪婪匹配),但嵌套循环在大文本下存在严重性能隐患,必须优化。
- .github/workflows/auto-review.yml L189: O(n²) 复杂度风险:双层循环在最坏情况下(50KB 文本,所有字符都是
{)会产生 25 亿次迭代,可能导致 GitHub Actions 超时。
修复方案:限制单次尝试的最大长度或先用正则找候选区间。
import json, sys, re
data = json.load(sys.stdin)
result = data.get('result', {})
text = result.get('output', '') if isinstance(result, dict) else str(result)
# 先用正则找所有可能的 JSON 区间(非贪婪匹配)
parsed = None
for match in re.finditer(r'\{[^{}]*(?:\{[^{}]*\}[^{}]*)*\}', text):
try:
candidate = json.loads(match.group())
if isinstance(candidate, dict) and 'approved' in candidate:
parsed = candidate
break # 找到第一个就停止
except (json.JSONDecodeError, ValueError, TypeError):
continue
if parsed:
print(json.dumps(parsed))
else:
print(json.dumps({'approved': False, 'summary': text[:2000], 'comments': []}))或者限制搜索范围(假设有效 JSON 不超过 10KB):
for i, ch in enumerate(text):
if ch == '{':
max_end = min(len(text), i + 10240) # 限制单次尝试最大 10KB
for j in range(max_end, i, -1):
# ...- .github/workflows/auto-review.yml L194: 异常处理不完整:只捕获了
json.JSONDecodeError和ValueError,但json.loads()在某些边界情况下可能抛出TypeError(如传入 None)。
修复方案:统一捕获 Exception 或明确添加 TypeError。
except (json.JSONDecodeError, ValueError, TypeError):
continue- .github/workflows/auto-review.yml L189: 多 JSON 对象时的边界行为:当文本中有多个包含
approved的 JSON 对象时,当前逻辑返回第一个找到的。但根据审查场景,最后一个 JSON 对象更可能是最终结果(如 AI 输出先有草稿再有最终版)。
建议:改为找最后一个包含 approved 的对象,或优先选择同时包含 summary 和 comments 的完整对象。
parsed = None
for i, ch in enumerate(text):
if ch == '{':
for j in range(len(text), i, -1):
if text[j-1] == '}':
try:
candidate = json.loads(text[i:j])
if isinstance(candidate, dict) and 'approved' in candidate:
parsed = candidate # 不 break,继续找后面的
except (json.JSONDecodeError, ValueError, TypeError):
continue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
修复贪婪正则导致 approved:true 被误解析为 False 的问题。改用逐位置尝试解析,找包含 approved key 的 JSON 对象。