fix: P0 token 环境变量 + P1 fail_count 逻辑简化(姜维 Review)
CI / lint (pull_request) Successful in 7s
CI / test (pull_request) Successful in 10s
CI / notify-on-failure (pull_request) Successful in 0s

This commit is contained in:
cfdaily
2026-06-13 23:43:20 +08:00
parent 3b9ad83405
commit a5d5d2d974
+9 -16
View File
@@ -7,6 +7,7 @@ from __future__ import annotations
import json
import logging
import os
import urllib.request
from pathlib import Path
from typing import Dict, List
@@ -23,10 +24,7 @@ logger = logging.getLogger("moziplus-v2.handler.toolchain")
# ---------------------------------------------------------------------------
_GITEA_BASE = "http://192.168.2.154:3000/api/v1"
_GITEA_TOKEN = "a6d596b826f4bfeaf983ef4d25ac25dab95bbc4e"
# 业务失败连续次数阈值,超过则升级为系统失败
_BUSINESS_FAIL_THRESHOLD = 3
_GITEA_TOKEN = os.environ.get("GITEA_TOKEN", "")
# action_type → action_hint 映射
_ACTION_HINTS: Dict[str, str] = {
@@ -308,22 +306,16 @@ class ToolchainHandler(BaseTaskHandler):
).fetchone()
if row and row["must_haves"]:
meta = json.loads(row["must_haves"])
# 统计该 task 的业务失败次数
fail_count = conn.execute(
"SELECT COUNT(*) FROM events WHERE task_id=? "
"AND event_type='status_change' AND payload LIKE '%failed%'",
(task_id,)
).fetchone()[0]
conn.close()
except Exception:
fail_count = 0
pass
action_type = meta.get("action_type", "")
context_data = meta.get("context", {})
assignee = meta.get("assignee", "") or meta.get("from", "")
# 三分路决策
route = self._classify_failure(verify, fail_count)
route = self._classify_failure(verify)
if route == "business":
self._handle_business_failure(
@@ -335,14 +327,11 @@ class ToolchainHandler(BaseTaskHandler):
self._handle_infrastructure_failure(
task_id, agent_id, verify, db_path)
def _classify_failure(self, verify: VerifyResult, fail_count: int) -> str:
def _classify_failure(self, verify: VerifyResult) -> str:
"""分类失败类型:business / system / infrastructure"""
# verify_error 或 DB 不可用 → 基础设施失败
if verify.reason == "verify_error":
return "infrastructure"
# 连续业务失败超过阈值 → 升级为系统失败
if fail_count >= _BUSINESS_FAIL_THRESHOLD:
return "system"
# 默认:业务失败
return "business"
@@ -450,6 +439,8 @@ class ToolchainHandler(BaseTaskHandler):
self, repo: str, pr_number: int, body: str,
) -> bool:
"""在 PR/Issue 上创建 comment。返回是否成功。"""
if not _GITEA_TOKEN:
return False
payload = json.dumps({"body": body}, ensure_ascii=False).encode("utf-8")
try:
req = urllib.request.Request(
@@ -471,6 +462,8 @@ class ToolchainHandler(BaseTaskHandler):
assignees: list = None,
) -> bool:
"""创建 Gitea Issue。返回是否成功。"""
if not _GITEA_TOKEN:
return False
data = {"title": title, "body": body}
if assignees:
data["assignees"] = assignees