fix(lint): 修复 PR #14 引入的 lint 回退 (119→0)
PR #14 从旧分支复制文件导致回退了 PR #10 的 lint 修复。 修复内容: - autoflake 移除未使用导入/变量 - autopep8 修复缩进/空格 - 手动修复 F821(pathlib→Path), F541(f-string), F841(未使用变量) - 所有修复均通过 flake8 --max-line-length=120 --extend-ignore=E501 检查 (0 errors)
This commit is contained in:
@@ -4,7 +4,7 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import re
|
||||
from dataclasses import dataclass, field
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
@@ -38,7 +38,9 @@ class GuardrailEngine:
|
||||
data = yaml.safe_load(f)
|
||||
self.rules = data.get("rules", [])
|
||||
self.settings = data.get("settings", {"enabled": True})
|
||||
logger.info("Loaded %d guardrail rules from %s", len(self.rules), config_path)
|
||||
logger.info(
|
||||
"Loaded %d guardrail rules from %s", len(
|
||||
self.rules), config_path)
|
||||
|
||||
def check_task(self, task: Any) -> List[GuardrailViolation]:
|
||||
"""检查 Task 是否触犯安全红线(调度前调用)"""
|
||||
@@ -95,7 +97,8 @@ class GuardrailEngine:
|
||||
|
||||
return violations
|
||||
|
||||
def check_token_usage(self, token_count: int) -> Optional[GuardrailViolation]:
|
||||
def check_token_usage(
|
||||
self, token_count: int) -> Optional[GuardrailViolation]:
|
||||
"""检查 Token 消耗是否超标"""
|
||||
if not self.settings.get("enabled", True):
|
||||
return None
|
||||
@@ -103,7 +106,10 @@ class GuardrailEngine:
|
||||
for rule in self.rules:
|
||||
if rule["id"] != "high_token_usage":
|
||||
continue
|
||||
threshold = rule.get("triggers", [{}])[0].get("token_threshold", 100000)
|
||||
threshold = rule.get(
|
||||
"triggers", [
|
||||
{}])[0].get(
|
||||
"token_threshold", 100000)
|
||||
if token_count > threshold:
|
||||
return GuardrailViolation(
|
||||
rule_id=rule["id"],
|
||||
@@ -114,7 +120,8 @@ class GuardrailEngine:
|
||||
)
|
||||
return None
|
||||
|
||||
def check_consecutive_failure(self, failure_count: int) -> Optional[GuardrailViolation]:
|
||||
def check_consecutive_failure(
|
||||
self, failure_count: int) -> Optional[GuardrailViolation]:
|
||||
"""检查连续失败次数"""
|
||||
if not self.settings.get("enabled", True):
|
||||
return None
|
||||
@@ -122,7 +129,10 @@ class GuardrailEngine:
|
||||
for rule in self.rules:
|
||||
if rule["id"] != "consecutive_failure":
|
||||
continue
|
||||
threshold = rule.get("triggers", [{}])[0].get("consecutive_failures", 3)
|
||||
threshold = rule.get(
|
||||
"triggers", [
|
||||
{}])[0].get(
|
||||
"consecutive_failures", 3)
|
||||
if failure_count >= threshold:
|
||||
return GuardrailViolation(
|
||||
rule_id=rule["id"],
|
||||
|
||||
Reference in New Issue
Block a user