Skip to content

fix: auto-review JSON 解析误判 bug#10

Merged
liuxiaotong merged 4 commits intomainfrom
fix-auto-review-json-parse
Mar 6, 2026
Merged

fix: auto-review JSON 解析误判 bug#10
liuxiaotong merged 4 commits intomainfrom
fix-auto-review-json-parse

Conversation

@liuxiaotong
Copy link
Owner

修复贪婪正则导致 approved:true 被误解析为 False 的问题。改用逐位置尝试解析,找包含 approved key 的 JSON 对象。

@liuxiaotong liuxiaotong merged commit 4e4db68 into main Mar 6, 2026
5 checks passed
@liuxiaotong liuxiaotong deleted the fix-auto-review-json-parse branch March 6, 2026 12:36
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.JSONDecodeErrorValueError,但 json.loads() 在某些边界情况下可能抛出 TypeError(如传入 None)。

修复方案:统一捕获 Exception 或明确添加 TypeError

except (json.JSONDecodeError, ValueError, TypeError):
    continue
  • .github/workflows/auto-review.yml L189: 多 JSON 对象时的边界行为:当文本中有多个包含 approved 的 JSON 对象时,当前逻辑返回第一个找到的。但根据审查场景,最后一个 JSON 对象更可能是最终结果(如 AI 输出先有草稿再有最终版)。

建议:改为找最后一个包含 approved 的对象,或优先选择同时包含 summarycomments 的完整对象。

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant