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:
+64
-18
@@ -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) {
|
||||
|
||||
+98
@@ -43,6 +43,16 @@ class AppearancePatentLlmClientHttpTest {
|
||||
private volatile boolean crossAssignFirstTitleItem = false;
|
||||
private volatile boolean reverseTitleItems = false;
|
||||
private volatile boolean dropLastAppearanceItem = false;
|
||||
/**
|
||||
* 正文形状钩子:模拟「HTTP 200 但正文按批量协议读不出来」的上游形状。
|
||||
* garbled_batch 只污染多行请求(补检的单行请求照常成功),garbled_all 连单行也污染,
|
||||
* top_level_array 是 2026-10-05 21:09 生产实测到的形状——条目自带 i,只是没包在 items 里。
|
||||
*/
|
||||
private volatile String appearanceBodyShape = "normal";
|
||||
private volatile String titleBodyShape = "normal";
|
||||
|
||||
/** 没有任何 JSON 结构的正文:readBatchRoot 两次解析都会抛,等同生产那 23 次失败。 */
|
||||
private static final String GARBLED_BODY = "外观判定结论:第一件商品无侵权,第二件商品无侵权。以上为纯文本描述。";
|
||||
|
||||
private AppearancePatentLlmClient client;
|
||||
|
||||
@@ -155,6 +165,9 @@ class AppearancePatentLlmClientHttpTest {
|
||||
List<String> texts = new ArrayList<>();
|
||||
parseIndexedLines(userTextOf(request), indexes, texts);
|
||||
boolean batch = indexes.size() > 1;
|
||||
if (isGarbledBody(titleBodyShape, indexes.size())) {
|
||||
return GARBLED_BODY;
|
||||
}
|
||||
List<Map<String, Object>> items = new ArrayList<>();
|
||||
for (int i = 0; i < indexes.size(); i++) {
|
||||
if (batch && dropLastTitleItem && i == indexes.size() - 1) {
|
||||
@@ -180,6 +193,9 @@ class AppearancePatentLlmClientHttpTest {
|
||||
List<Integer> indexes = new ArrayList<>();
|
||||
List<String> texts = new ArrayList<>();
|
||||
parseIndexedLines(appearanceUserTextOf(request), indexes, texts);
|
||||
if (isGarbledBody(appearanceBodyShape, indexes.size())) {
|
||||
return GARBLED_BODY;
|
||||
}
|
||||
List<Map<String, Object>> items = new ArrayList<>();
|
||||
for (int i = 0; i < indexes.size(); i++) {
|
||||
if (indexes.size() > 1 && dropLastAppearanceItem && i == indexes.size() - 1) {
|
||||
@@ -189,11 +205,19 @@ class AppearancePatentLlmClientHttpTest {
|
||||
"appearance_status", texts.get(i).contains("原创个性杯") ? "侵权" : "无侵权",
|
||||
"appearance_reason", appearanceReasonFor(texts.get(i))));
|
||||
}
|
||||
if ("top_level_array".equals(appearanceBodyShape)) {
|
||||
return objectMapper.writeValueAsString(items);
|
||||
}
|
||||
String json = objectMapper.writeValueAsString(Map.of("items", items));
|
||||
boolean dirtySingle = indexes.size() <= 1 && !texts.isEmpty() && texts.get(0).contains("原创个性杯");
|
||||
return dirtySingle ? "```json\n" + json + "\n```" : json;
|
||||
}
|
||||
|
||||
/** 污染范围按请求行数决定:只污染批量才能验证「整批读不出来时改为逐行补检」。 */
|
||||
private static boolean isGarbledBody(String shape, int requestedRows) {
|
||||
return "garbled_all".equals(shape) || ("garbled_batch".equals(shape) && requestedRows > 1);
|
||||
}
|
||||
|
||||
private String appearanceReasonFor(String text) {
|
||||
return text.contains("原创个性杯")
|
||||
? "【视觉拆解】:特殊造型\n【判定依据】:高度相似知名设计"
|
||||
@@ -555,6 +579,80 @@ class AppearancePatentLlmClientHttpTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 外观批量正文读不出来(HTTP 200)=和漏行同一类失真:必须逐行补检把结论拿回来,
|
||||
* 不允许一次上游抖动把整批行直接打成「外观识别异常」——那正是报表里「有行没结论」的来源,
|
||||
* 而且外层会把这批行标成已检测,用户重发任务也捞不回来。
|
||||
*/
|
||||
@Test
|
||||
void appearanceBatchGarbledBodyRedoesRowsIndividually() {
|
||||
appearanceBodyShape = "garbled_batch";
|
||||
List<AppearancePatentResultRowDto> rows = List.of(
|
||||
row("1", "F001", "普通直筒杯A", "", "https://img.example.com/f1.jpg"),
|
||||
row("2", "F002", "普通直筒杯B", "", "https://img.example.com/f2.jpg"),
|
||||
row("3", "F003", "原创个性杯", "", "https://img.example.com/f3.jpg"));
|
||||
|
||||
List<AppearancePatentResultRowDto> result = client.inspectRows(rows, null, "test-key");
|
||||
|
||||
assertThat(imageRequestCount.get()).isEqualTo(4);
|
||||
assertThat(result.get(0).getAppearanceRisk()).isEqualTo("无侵权");
|
||||
assertThat(result.get(1).getAppearanceRisk()).isEqualTo("无侵权");
|
||||
assertThat(result.get(2).getAppearanceRisk()).isEqualTo("侵权");
|
||||
}
|
||||
|
||||
/** 补检有上界:单行补检也读不出来时,每行只做一次补检就落回退,绝不套娃重发。 */
|
||||
@Test
|
||||
void appearanceBatchGarbledBodyRedoIsBoundedToOnePass() {
|
||||
appearanceBodyShape = "garbled_all";
|
||||
List<AppearancePatentResultRowDto> rows = new ArrayList<>();
|
||||
for (int i = 1; i <= 3; i++) {
|
||||
rows.add(row(String.valueOf(i), "G00" + i, "普通直筒杯" + i, "", "https://img.example.com/g" + i + ".jpg"));
|
||||
}
|
||||
|
||||
List<AppearancePatentResultRowDto> result = client.inspectRows(rows, null, "test-key");
|
||||
|
||||
assertThat(imageRequestCount.get()).isEqualTo(4);
|
||||
for (AppearancePatentResultRowDto item : result) {
|
||||
assertThat(item.getAppearanceRisk()).isEqualTo("外观识别异常");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 顶层数组正文(生产实测 2026-10-05 21:09):条目自带 i,只是没包在 items 里,
|
||||
* 按既有整串解包口径(parseResults)同样收下,不许白扔一整批。
|
||||
*/
|
||||
@Test
|
||||
void appearanceBatchTopLevelArrayBodyIsRead() {
|
||||
appearanceBodyShape = "top_level_array";
|
||||
List<AppearancePatentResultRowDto> rows = List.of(
|
||||
row("1", "H001", "普通直筒杯A", "", "https://img.example.com/h1.jpg"),
|
||||
row("2", "H002", "原创个性杯", "", "https://img.example.com/h2.jpg"));
|
||||
|
||||
List<AppearancePatentResultRowDto> result = client.inspectRows(rows, null, "test-key");
|
||||
|
||||
assertThat(imageRequestCount.get()).isEqualTo(1);
|
||||
assertThat(result.get(0).getAppearanceRisk()).isEqualTo("无侵权");
|
||||
assertThat(result.get(1).getAppearanceRisk()).isEqualTo("侵权");
|
||||
assertThat(result.get(1).getConclusion()).isEqualTo("侵权");
|
||||
}
|
||||
|
||||
/** 商标提取那一路共用同一套批次调度:整批正文读不出来时同样逐行补检。 */
|
||||
@Test
|
||||
void titleBatchGarbledBodyRedoesRowsIndividually() {
|
||||
titleBodyShape = "garbled_batch";
|
||||
List<AppearancePatentResultRowDto> rows = List.of(
|
||||
row("1", "I001", "Apple Magic Case", "", "https://img.example.com/i1.jpg"),
|
||||
row("2", "I002", "Nike Air 鞋", "", "https://img.example.com/i2.jpg"));
|
||||
|
||||
List<AppearancePatentResultRowDto> result = client.inspectRows(rows, null, "test-key");
|
||||
|
||||
assertThat(textRequestCount.get()).isEqualTo(3);
|
||||
assertThat(result.get(0).getTitleReason()).isEqualTo("Apple");
|
||||
assertThat(result.get(1).getTitleReason()).isEqualTo("Nike");
|
||||
assertThat(result.get(0).getAppearanceRisk()).isEqualTo("无侵权");
|
||||
assertThat(result.get(1).getAppearanceRisk()).isEqualTo("无侵权");
|
||||
}
|
||||
|
||||
private static int countOccurrences(String haystack, String needle) {
|
||||
int count = 0;
|
||||
for (int i = haystack.indexOf(needle); i >= 0; i = haystack.indexOf(needle, i + needle.length())) {
|
||||
|
||||
Reference in New Issue
Block a user