fix(appearance-patent): LLM 连接层故障短路,行级写明服务不可达真因

invokeChat 的重试阶梯原先不区分故障层:网关根本连不上也把 llmRetryTimes
打满。照 BrandCheckClient 的 SERVICE_UNREACHABLE 同口径补短路——同一次
调用连续 2 次连接未建立(ConnectException/NoRouteToHost/UnknownHost,
顺 cause 链)且中间无一次成功 ⇒ 停止重试,行级原因写成「LLM 服务不可达
(连接未建立,非本行数据问题)」。读超时与 5xx 故意不算,非连接失败
清零证据。结论一字不变,差别只在耗时与归因文案。
新增 2 例互为非零对照:死端口必须在第 2 次连接失败处停(第 3 次不发);
连得上但 5xx 要跑满整个阶梯。
This commit is contained in:
2026-10-23 18:00:34 +08:00
parent 70b3be63a9
commit b65b709700
2 changed files with 75 additions and 0 deletions
@@ -293,6 +293,39 @@ class AppearancePatentLlmClientHttpTest {
localBrandCheckClient(), mock(UserSecretUsageService.class));
}
/** 连接层故障短路:网关根本连不上时,重试阶梯必须在证据数处停下,行级拿到可读的真因。 */
@Test
void unreachableGatewayShortCircuitsLadderAndSurfacesReason() {
AppearancePatentLlmClient deadHostClient = buildClient(p -> {
p.setLlmHost("http://127.0.0.1:1");
p.setLlmRetryTimes(3);
});
List<AppearancePatentResultRowDto> result = deadHostClient.inspectRows(
List.of(row("9", "B009", "任意标题", "", "https://img.example.com/9.jpg")), null, "test-key");
assertThat(result).hasSize(1);
AppearancePatentResultRowDto failed = result.get(0);
// 行级原因走标题回退链路(invokeTitleBatchSafely 把异常折进 titleErrors)
assertThat(failed.getTitleReason()).contains("LLM 服务不可达(连接未建立,非本行数据问题)");
assertThat(failed.getTitleReason()).doesNotContain("LLM http ");
// 短路发生在第 2 次连接失败,第 3 次尝试不再发出:连接层故障重试救不回来
assertThat(textRequestCount.get()).isZero();
assertThat(imageRequestCount.get()).isZero();
}
/** 反向对照:连得上只是 5xx(非连接层故障)不得触发短路,重试阶梯要跑满。 */
@Test
void httpFailureKeepsFullRetryLadder() {
failTextRequests = true;
AppearancePatentLlmClient threeAttemptClient = buildClient(p -> p.setLlmRetryTimes(3));
threeAttemptClient.inspectRows(
List.of(row("10", "B010", "任意标题", "", "https://img.example.com/10.jpg")), null, "test-key");
assertThat(textRequestCount.get()).isEqualTo(3);
}
private boolean messagesBodyContains(HttpExchange exchange, String text) {
return capturedBodies.stream().anyMatch(b -> b.contains(text));
}