fix(外观专利): 批量正文读不出来改走单行补检,不再按传输失败整批记死

上游回了 200 但正文不是批量协议(纯文本、或缺 items 数组)时,原来和「请求根本没通」
落进同一个分支:整批 5 行一起记失败、外观请求也不再发出,报表里就是一批没有结论的行
(10-05 21:00-21:32 生产 23 次,约 115 行)。现按异常类型分成内容失败与传输失败两类,
由代码判定而非调用方猜文案:内容失败交给既有单行补检一轮,传输失败仍整批收口、绝不逐行
放大成本。顺带接受「条目齐全但没有 items 信封」的顶层数组形状(生产实见 2 例)。

护栏:HttpTest 加 4 条用例(批内乱码补检、全量乱码只补一轮、顶层数组照读、商标批乱码);
变异 5/5 KILLED,其中 M5 是反向护栏——把分类写死成内容失败也会被既有 3 条用例杀。
货源 SimilarAsinLlmService 逐条核过:批量分类解析失败即走单行回退,无同形状缺陷,不改。
This commit is contained in:
2026-10-06 00:49:32 +08:00
parent 764ac21d30
commit e5e36aa713
2 changed files with 162 additions and 18 deletions
@@ -226,12 +226,13 @@ public class AppearancePatentLlmClient {
List<Integer> chunk,
String apiKey) {
try {
return new BatchOutcome<>(invokeTitleBatch(rows, chunk, apiKey), null);
return BatchOutcome.ok(invokeTitleBatch(rows, chunk, apiKey));
} catch (Exception ex) {
String message = failureMessage(ex);
log.warn("[appearance-patent] title batch failed rows={} firstAsin={} err={}",
chunk.size(), rows.get(chunk.get(0)).getAsin(), message);
return new BatchOutcome<>(Map.of(), message);
log.warn("[appearance-patent] title batch failed rows={} firstAsin={} content={} err={}",
chunk.size(), rows.get(chunk.get(0)).getAsin(),
ex instanceof BatchContentException, message);
return BatchOutcome.failed(ex, message);
}
}
@@ -240,18 +241,21 @@ public class AppearancePatentLlmClient {
String prompt,
String apiKey) {
try {
return new BatchOutcome<>(invokeAppearanceBatch(rows, chunk, prompt, apiKey), null);
return BatchOutcome.ok(invokeAppearanceBatch(rows, chunk, prompt, apiKey));
} catch (Exception ex) {
String message = failureMessage(ex);
log.warn("[appearance-patent] appearance batch failed rows={} firstAsin={} err={}",
chunk.size(), rows.get(chunk.get(0)).getAsin(), message);
return new BatchOutcome<>(Map.of(), message);
log.warn("[appearance-patent] appearance batch failed rows={} firstAsin={} content={} err={}",
chunk.size(), rows.get(chunk.get(0)).getAsin(),
ex instanceof BatchContentException, message);
return BatchOutcome.failed(ex, message);
}
}
/**
* 阶段调度:切批并发发请求 → 漏行/失真行单发补检一次 → 仍缺失的行记失败。
* 传输失败按整批记失败,绝不逐行重发(一次请求带走一批,劣化窗口下不会放大成 N 倍);
* 正文读不出来(contentFailure)按失真处理——整批行进 missing 走同一条补检路,
* 补检仍失败才落回退,代价是每行一次单图请求,换回的是行上有结论而不是「外观识别异常」。
* 补检请求不再套校验门,既保留今天对模型自身过度提取的容忍,也杜绝无限重做。
*/
private <T> void pass(List<Integer> indexes, int batchSize, BatchInvoker<T> invoker,
@@ -266,11 +270,13 @@ public class AppearancePatentLlmClient {
for (int c = 0; c < chunks.size(); c++) {
List<Integer> chunk = chunks.get(c);
BatchOutcome<T> outcome = outcomes.get(c);
if (outcome.transportError() != null) {
if (outcome.failure() != null) {
for (int index : chunk) {
errors.put(index, outcome.transportError());
errors.put(index, outcome.failure());
}
if (!outcome.contentFailure()) {
continue;
}
continue;
}
for (int index : chunk) {
T value = outcome.byIndex().get(index);
@@ -298,7 +304,34 @@ public class AppearancePatentLlmClient {
BatchOutcome<T> invoke(List<Integer> chunk);
}
private record BatchOutcome<T>(Map<Integer, T> byIndex, String transportError) {
/**
* 一批请求的产物。failure 非空=这批没拿到可用结论,contentFailure 决定后续怎么走:
*
* <ul>
* <li>contentFailure=true:上游回了 200、正文也拿到了,只是按批量协议读不出来。
* 这是模型失真(实测到它跳过 items 信封直接回数组),换单行请求重问往往就读得出来,
* 所以整批行要进补检,而不是按失败收口——2026-10-05 报表「有行没结论」就是这么来的。</li>
* <li>contentFailure=false:传输层失败(超时/5xx/响应体不是 JSON)。劣化窗口里逐行重发
* 只会把一次抖动放大成 N 次请求,维持整批记失败。</li>
* </ul>
*/
private record BatchOutcome<T>(Map<Integer, T> byIndex, String failure, boolean contentFailure) {
static <T> BatchOutcome<T> ok(Map<Integer, T> byIndex) {
return new BatchOutcome<>(byIndex, null, false);
}
/** 失败分类由异常类型决定,不允许调用方各自猜文案。 */
static <T> BatchOutcome<T> failed(Exception ex, String message) {
return new BatchOutcome<>(Map.of(), message, ex instanceof BatchContentException);
}
}
/** 正文到手但按批量协议读不出来:交给单行补检,不按传输失败整批记死。 */
private static final class BatchContentException extends RuntimeException {
BatchContentException(String message, Throwable cause) {
super(message, cause);
}
}
private static List<List<Integer>> partition(List<Integer> indexes, int batchSize) {
@@ -326,7 +359,7 @@ public class AppearancePatentLlmClient {
}
} catch (InterruptedException interruptedException) {
Thread.currentThread().interrupt();
return new BatchOutcome<T>(Map.of(), "LLM 批次等待被中断");
return new BatchOutcome<T>(Map.of(), "LLM 批次等待被中断", false);
}
};
futures.add(CompletableFuture.supplyAsync(SecretUsageContext.wrap(task, usageContext), executor));
@@ -336,9 +369,10 @@ public class AppearancePatentLlmClient {
try {
outcomes.add(futures.get(i).join());
} catch (Exception ex) {
// 调度层炸了(invoker 本该自己 catch 干净):按传输失败收口,宁可整批记失败也不逐行放大
log.warn("[appearance-patent] llm batch crashed rows={} err={}",
chunks.get(i).size(), failureMessage(ex));
outcomes.add(new BatchOutcome<T>(Map.of(), failureMessage(ex)));
outcomes.add(new BatchOutcome<T>(Map.of(), failureMessage(ex), false));
}
}
return outcomes;
@@ -495,12 +529,24 @@ public class AppearancePatentLlmClient {
&& !normalize(judgement.reason()).isBlank();
}
/** 批量响应解包:序号 → 条目,按序号对账而不是按数组位置(模型可能漏项或乱序)。 */
/**
* 批量响应解包:序号 → 条目,按序号对账而不是按数组位置(模型可能漏项或乱序)。
* 顶层直接回数组同样收下——条目本来就带 i,items 信封只是提示词要求的包装
* (生产实测 2026-10-01 22:12、10-05 21:09 各一次,整批完好的结论因缺信封被丢弃)。
*
* <p>本方法抛出的一律是 {@link BatchContentException}:正文已经到手,只是按协议读不出来,
* 与传输失败区分对待才有单行补检。
*/
private Map<Integer, JsonNode> parseIndexedItems(String content) {
JsonNode root = readBatchRoot(content);
JsonNode items = root.path("items");
JsonNode root;
try {
root = readBatchRoot(content);
} catch (RuntimeException ex) {
throw new BatchContentException(failureMessage(ex), ex);
}
JsonNode items = root.isArray() ? root : root.path("items");
if (!items.isArray()) {
throw new IllegalStateException("LLM 批量响应缺少 items 数组");
throw new BatchContentException("LLM 批量响应缺少 items 数组", null);
}
Map<Integer, JsonNode> byIndex = new LinkedHashMap<>();
for (JsonNode item : items) {