test(14-01): 实现 LLM 调用的可恢复错误检测和重试逻辑
- 添加 _is_recoverable_error() 静态方法判断错误类型 - 可恢复错误: ConnectionError, Timeout, HTTPError (429/500/502/503/504) - 不可恢复错误: 其他 HTTPError (4xx), ValidationError - 重试逻辑: 仅对可恢复错误重试 1 次,立即重试(无延迟) - 改进日志记录: 重试用 info 级别,降级用 warning 级别 - 降级时记录具体错误类型和消息 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -471,8 +471,11 @@ class TestParseFailureRetry:
|
||||
# 验证调用了两次 LLM(初始 + 1 次重试)
|
||||
assert mock_client.generate.call_count == 2
|
||||
|
||||
# 验证日志包含重试信息
|
||||
assert any("重试" in record.message for record in caplog.records)
|
||||
# 验证日志包含重试信息(可能是"重试"或"retrying")
|
||||
assert any(
|
||||
"重试" in record.message or "retrying" in record.message.lower()
|
||||
for record in caplog.records
|
||||
)
|
||||
|
||||
def test_validation_error_no_retry(self, caplog):
|
||||
"""测试 ValidationError(Pydantic 验证失败)直接降级,不重试"""
|
||||
@@ -510,8 +513,10 @@ class TestParseFailureRetry:
|
||||
assert mock_client.generate.call_count == 1
|
||||
|
||||
# 验证日志包含验证失败信息
|
||||
assert any("Pydantic 验证失败" in record.message or "验证失败" in record.message
|
||||
for record in caplog.records)
|
||||
assert any(
|
||||
"ValidationError" in record.message or "验证失败" in record.message
|
||||
for record in caplog.records
|
||||
)
|
||||
|
||||
def test_parse_error_logs_llm_output(self, caplog):
|
||||
"""测试解析失败时记录 LLM 输出片段(前 500 字符)"""
|
||||
@@ -528,17 +533,17 @@ class TestParseFailureRetry:
|
||||
|
||||
segments = [Segment(1, 0.0, 1.0, "测试")]
|
||||
|
||||
with caplog.at_level(logging.ERROR):
|
||||
with caplog.at_level(logging.WARNING):
|
||||
result = strategy.segment(segments, {"title": "测试"})
|
||||
|
||||
# 验证日志包含 LLM 输出片段
|
||||
error_logs = [record for record in caplog.records if record.levelno == logging.ERROR]
|
||||
assert len(error_logs) > 0
|
||||
# 验证日志包含 LLM 输出片段(使用 WARNING 级别)
|
||||
warning_logs = [record for record in caplog.records if record.levelno == logging.WARNING]
|
||||
assert len(warning_logs) > 0
|
||||
|
||||
# 验证日志包含 LLM 输出片段信息
|
||||
assert any(
|
||||
"LLM 输出片段" in record.message or "原始 LLM 输出" in record.message
|
||||
for record in error_logs
|
||||
"LLM 输出片段" in record.message
|
||||
for record in warning_logs
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user