Skip to content

fix: auto-review task poll 403#12

Merged
liuxiaotong merged 1 commit intomainfrom
fix/auto-review-task-poll-403
Mar 7, 2026
Merged

fix: auto-review task poll 403#12
liuxiaotong merged 1 commit intomainfrom
fix/auto-review-task-poll-403

Conversation

@liuxiaotong
Copy link
Owner

Summary

  • 提交 review 任务时在 payload 中传入 user_id: github-actions,使任务有 owner
  • 轮询任务状态的 URL 加 ?user_id=github-actions 查询参数,解决 403 拒绝

Root Cause

auto-review 提交 review 任务时没传 user_id,导致任务无 owner,轮询 /tasks/{task_id} 时被 403 拒绝。

提交 review 任务时传 user_id,轮询时带 user_id 查询参数,
解决无 owner 任务被 403 拒绝的问题。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions
Copy link

github-actions bot commented Mar 7, 2026

Auto Review (Round 1) — ❌ Changes Requested

第 1 轮审查:Request Changes。核心修复方向正确(传入 user_id 解决 403),但存在 1 个 Critical 问题(硬编码 user_id 可能导致跨 PR 任务泄露)和 2 个 Warning(错误处理缺失、轮询超时配置不合理)。

核心问题:硬编码 'github-actions' 作为 user_id 会让所有 PR 的审查任务共享同一个 owner,可能导致 PR A 的轮询拿到 PR B 的结果。建议改用 'github-actions-pr-{PR_NUMBER}' 或 'github-actions-{GITHUB_RUN_ID}' 确保任务隔离。

  • .github/workflows/auto-review.yml L134: 硬编码 user_id='github-actions' 存在任务泄露风险。所有 PR 的审查任务共享同一个 user_id,可能导致:
  1. PR A 的轮询请求拿到 PR B 的任务结果(如果 task_id 碰撞或缓存混乱)
  2. 无法区分不同 PR 的任务归属,排查问题时难以定位

修复方案:使用 PR 号或 run ID 作为 user_id 后缀,确保任务隔离:

user_id="github-actions-pr-${{ github.event.pull_request.number }}"
#
user_id="github-actions-${{ github.run_id }}"

然后在 Python 脚本中:

print(json.dumps({'task': task, 'format': 'json', 'user_id': os.environ['USER_ID']}))

在轮询 URL 中同样使用该变量:

curl -s "https://crew.knowlyr.com/tasks/${TASK_ID}?user_id=${USER_ID}"
  • .github/workflows/auto-review.yml L159: 轮询逻辑缺少错误处理,可能导致静默失败:
  1. curl 请求失败时 POLL_RESP 为空,但继续执行 python 解析会报错
  2. STATUS 解析失败时默认为 'unknown',但没有日志输出,难以排查
  3. 轮询超时后没有明确的失败提示

建议增强错误处理:

POLL_RESP=$(curl -s -w "\n%{http_code}" "https://crew.knowlyr.com/tasks/${TASK_ID}?user_id=github-actions" \
  -H "Authorization: Bearer ${CREW_API_TOKEN}")
HTTP_CODE=$(echo "$POLL_RESP" | tail -n1)
if [ "$HTTP_CODE" != "200" ]; then
  echo "::error::轮询失败 (HTTP $HTTP_CODE): $(echo "$POLL_RESP" | head -n-1)"
  exit 1
fi
STATUS=$(echo "$POLL_RESP" | head -n-1 | python3 -c "import json,sys; print(json.load(sys.stdin).get('status','unknown'))" 2>/dev/null)
if [ "$STATUS" = "unknown" ]; then
  echo "::warning::无法解析任务状态,响应: $(echo "$POLL_RESP" | head -n-1)"
fi
  • .github/workflows/auto-review.yml L154: 轮询配置不合理,可能导致超时或资源浪费:
  1. POLL_TIMEOUT=300(5 分钟)对于代码审查任务可能不够(复杂 PR 可能需要更长时间)
  2. POLL_INTERVAL=5(5 秒)过于频繁,对服务端造成不必要的压力

建议调整为:

POLL_TIMEOUT=600  # 10 分钟,给复杂审查留足时间
POLL_INTERVAL=10  # 10 秒间隔,减少服务端压力

或者使用指数退避策略:

POLL_INTERVAL=5
while [ $POLL_ELAPSED -lt $POLL_TIMEOUT ]; do
  sleep $POLL_INTERVAL
  POLL_ELAPSED=$((POLL_ELAPSED + POLL_INTERVAL))
  # ... 轮询逻辑 ...
  POLL_INTERVAL=$((POLL_INTERVAL * 2))  # 指数退避:5s -> 10s -> 20s ...
  if [ $POLL_INTERVAL -gt 30 ]; then POLL_INTERVAL=30; fi  # 最大 30s
done
  • .github/workflows/auto-review.yml L134: user_id 硬编码在两处(提交任务和轮询),违反 DRY 原则。建议在 workflow 顶部定义环境变量:
env:
  CREW_USER_ID: github-actions-pr-${{ github.event.pull_request.number }}

然后在脚本中引用 $CREW_USER_ID,便于后续统一修改。

@liuxiaotong liuxiaotong merged commit 52a51d1 into main Mar 7, 2026
4 of 5 checks passed
@liuxiaotong liuxiaotong deleted the fix/auto-review-task-poll-403 branch March 7, 2026 07:52
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