fix(llm): 余额耗尽按「余额欠费」归因且不再重试进冷却

- 新增 LlmBalanceGuard:识别 insufficient_user_quota 等上游余额/额度字样
- SimilarAsin/AppearancePatent 客户端命中后一次就停,不落进不可达/冷却分支
- 单测:识别口径 3 例 + 两客户端各 1 例(只发 1 次、文案含余额欠费)
This commit is contained in:
2026-10-27 10:04:03 +08:00
parent e0abd33ed4
commit f3445c21c8
6 changed files with 169 additions and 0 deletions
@@ -0,0 +1,40 @@
package com.nanri.aiimage.common.retry;
import org.junit.jupiter.api.Test;
import java.net.ConnectException;
import static org.assertj.core.api.Assertions.assertThat;
/**
* 余额欠费识别口径(2026-10-27):只认上游余额/额度专属字样,
* 连接层故障、读超时、5xx 与 429 限流都不得被误判成欠费。
*/
class LlmBalanceGuardTest {
/** 现网原文(A/B 机 java-server 2026-10-27 实测),必须命中。 */
@Test
void detectsObservedGatewayQuotaBody() {
IllegalStateException observed = new IllegalStateException(
"LLM http 403: {\"error\":{\"message\":\"预扣费额度失败, 用户[151246]剩余额度: ฿0.029168, "
+ "需要预扣费额度: ฿0.032402\",\"type\":\"new_api_error\",\"code\":\"insufficient_user_quota\"}}");
assertThat(LlmBalanceGuard.isQuotaExhausted(observed)).isTrue();
}
@Test
void detectsQuotaMarkersThroughCauseChain() {
IllegalStateException wrapped = new IllegalStateException("batch failed",
new RuntimeException("upstream says insufficient_quota"));
assertThat(LlmBalanceGuard.isQuotaExhausted(wrapped)).isTrue();
}
@Test
void transientFailuresAreNotQuotaExhaustion() {
assertThat(LlmBalanceGuard.isQuotaExhausted(new IllegalStateException("LLM http 502: bad gateway"))).isFalse();
assertThat(LlmBalanceGuard.isQuotaExhausted(new IllegalStateException("LLM empty response"))).isFalse();
assertThat(LlmBalanceGuard.isQuotaExhausted(new IllegalStateException("429 rate limit exceeded"))).isFalse();
assertThat(LlmBalanceGuard.isQuotaExhausted(new ConnectException("Connection refused"))).isFalse();
}
}