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
@@ -33,6 +33,10 @@ class SimilarAsinLlmClientHttpTest {
/** 请求前挂起时长:模拟"连上了但不应答"的读超时形状。 */
private volatile long responseDelayMillis = 0L;
private volatile boolean failWith502 = false;
private volatile boolean failWithQuota403 = false;
private static final String QUOTA_403_BODY = "{\"error\":{\"message\":\"预扣费额度失败, 用户[151246]剩余额度: "
+ "฿0.029168, 需要预扣费额度: ฿0.032402\",\"type\":\"new_api_error\",\"code\":\"insufficient_user_quota\"}}";
@BeforeEach
void setUp() throws IOException {
@@ -51,6 +55,10 @@ class SimilarAsinLlmClientHttpTest {
private void handleChat(HttpExchange exchange) throws IOException {
exchange.getRequestBody().readAllBytes();
requestCount.incrementAndGet();
if (failWithQuota403) {
respond(exchange, 403, QUOTA_403_BODY);
return;
}
long delay = responseDelayMillis;
if (delay > 0L) {
try {
@@ -94,6 +102,26 @@ class SimilarAsinLlmClientHttpTest {
assertThat(elapsedMs).isLessThan(12_000L);
}
/**
* 余额欠费(2026-10-27 现网 403 insufficient_user_quota):按"余额欠费"归因、一次就停,
* 既不跑满重试阶梯,也不得落进连接层不可达/冷却分支。
*/
@Test
void quotaExhaustionIsAttributedAsBalanceDueAndNotRetried() {
failWithQuota403 = true;
SimilarAsinLlmClient threeAttemptClient = buildClient(p -> p.setLlmRetryTimes(3));
assertThatThrownBy(() -> threeAttemptClient.invokeChat("test-model", "s", "u", "test-key"))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("LLM 服务余额欠费")
.hasMessageContaining("insufficient_user_quota")
.hasMessageNotContaining("LLM 服务不可达")
.hasMessageNotContaining("冷却中");
assertThat(requestCount.get()).isEqualTo(1);
assertThat(lastCooldown.isCoolingDown(lastHost, lastWindow)).isFalse();
}
/** 反向对照:连得上只是 5xx(非连接层故障)不得触发短路,重试阶梯要跑满。 */
@Test
void httpFailureKeepsFullRetryLadder() {