diff --git a/backend-java/src/main/java/com/nanri/aiimage/config/AppearancePatentProperties.java b/backend-java/src/main/java/com/nanri/aiimage/config/AppearancePatentProperties.java index 251e718b5..8c4b1d8d6 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/config/AppearancePatentProperties.java +++ b/backend-java/src/main/java/com/nanri/aiimage/config/AppearancePatentProperties.java @@ -79,8 +79,11 @@ public class AppearancePatentProperties { * 组装结果文件时要求的 LLM 覆盖率下限(0~1,<=0 关闭)。 * 低于该比例说明大批行根本没拿到结论(如 2026-09-20 任务 28805:904 行只检了 1 行却报成功), * 此时判任务失败并由失败通知告警,而不是交付一份看着完整、实际是复制结论的表。 - * 默认 0.5 只拦塌陷级异常;生产正常任务实测约 0.91(28798:723 行 658 行有结论), - * 需要更严时调高,调到 1.0 即要求全行有结论。 + * + *

2026-10-10 从 0.5 抬到 0.95:0.5 意味着半张表可以空着照样出文件。现网实测(10-06~10-09 + * 六份已交付 xlsx)空行比例 20.7%–51.3%,全部在 0.5 之上放行、且一条覆盖率告警都没打—— + * 因为旧分母按用户上传表的解析行数,用户表不带标题列时分母恒为 0。 + * 分母口径已改为「会写进结果文件的行集」,判据见 AppearancePatentRowOutcome。 */ - private double llmCoverageMinRatio = 0.5D; + private double llmCoverageMinRatio = 0.95D; } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentTaskCacheService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentTaskCacheService.java index bafb3e0a0..c62eda027 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentTaskCacheService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentTaskCacheService.java @@ -58,7 +58,7 @@ public class AppearancePatentTaskCacheService extends AbstractPendingRowTaskCach return first == null || first; } catch (Exception ex) { log.warn("[appearance-patent-cache] begin row inspection degraded taskId={} rowKey={} msg={}", taskId, rowKey, ex.getMessage()); - // Redis 降级时放行,由持久化层的 hasResolvedLlmFields 判据兜底去重。 + // Redis 降级时放行,由持久化层的 AppearancePatentRowOutcome.isJudged 判据兜底去重。 return true; } } @@ -102,9 +102,18 @@ public class AppearancePatentTaskCacheService extends AbstractPendingRowTaskCach /** * 撤销处理中标记(本批没拿到可用结果时调用):让下一轮立刻能重新申领,不等 15 分钟 TTL。 - * TTL 只用来兜住「进程在批中被杀」。 + * TTL 只用来兜住「进程在批中被杀」。完成标记**不动**——动它会让已经落库的行重跑并重复计费。 */ public void releaseRowInspection(Long taskId, List rowKeys) { + releaseRowInspection(taskId, rowKeys, false); + } + + /** + * @param alsoClearCompletionMark 交付缺口补检专用:结果落过库却又读不回来(分片被清、载荷过期)时, + * 这行既没结论也再申领不到,等于扣了次数永久空着;只有这条出口 + * 允许连完成标记一起撤,代价(可能重发一次 LLM)由调用方点名承担。 + */ + public void releaseRowInspection(Long taskId, List rowKeys, boolean alsoClearCompletionMark) { if (taskId == null || taskId <= 0 || rowKeys == null || rowKeys.isEmpty()) { return; } @@ -114,6 +123,9 @@ public class AppearancePatentTaskCacheService extends AbstractPendingRowTaskCach } try { stringRedisTemplate.delete(inFlightRowKey(taskId, rowKey)); + if (alsoClearCompletionMark) { + stringRedisTemplate.delete(processedRowKey(taskId, rowKey)); + } } catch (Exception ex) { log.warn("[appearance-patent-cache] release row inspection degraded taskId={} rowKey={} msg={}", taskId, rowKey, ex.getMessage()); diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentTaskService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentTaskService.java index 2508ef787..21d222cc9 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentTaskService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentTaskService.java @@ -35,6 +35,7 @@ import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentParse import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentTaskBatchVo; import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentTaskDetailVo; import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentTaskItemVo; +import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentRowOutcome; import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentSheetBuilder; import com.nanri.aiimage.modules.file.service.LocalFileStorageService; import com.nanri.aiimage.modules.file.service.oss.OssStorageService; @@ -944,8 +945,7 @@ public class AppearancePatentTaskService { private void submitLlmBatches(FileTaskEntity task, FileResultEntity result, TaskFileJobEntity job, - List chunks, - Map> allRowsByBaseId) { + List chunks) { if (chunks == null || chunks.isEmpty()) { return; } @@ -954,7 +954,7 @@ public class AppearancePatentTaskService { int batchSize = Math.max(1, properties.getLlmBatchSize()); ChunkRowKeyIndex rowKeyIndex = new ChunkRowKeyIndex(); List candidates = terminalizeRoundExhaustedRows( - task, allRowsByBaseId, collectPendingLlmCandidates(chunks, rowKeyIndex), rowKeyIndex); + task, collectPendingLlmCandidates(chunks, rowKeyIndex), rowKeyIndex); boolean flushRemainder = isResultSubmissionComplete(task.getId()); if (!flushRemainder && !candidates.isEmpty()) { LocalDateTime jobUpdatedAt = job.getUpdatedAt(); @@ -1007,7 +1007,7 @@ public class AppearancePatentTaskService { } List keysToRetry = new ArrayList<>(batchRowKeys); try { - Set landedKeys = mergeLlmRowsIntoChunks(task, allRowsByBaseId, llmRows, batchRows, rowKeyIndex); + Set landedKeys = mergeLlmRowsIntoChunks(task, llmRows, batchRows, rowKeyIndex); // 完成标记只能打在真正落库的行上——这是「发版重启会不会丢行」的分界线 if (batchAnswered) { List landed = batchRowKeys.stream().filter(landedKeys::contains).toList(); @@ -1046,7 +1046,6 @@ public class AppearancePatentTaskService { */ private List terminalizeRoundExhaustedRows( FileTaskEntity task, - Map> allRowsByBaseId, List candidates, ChunkRowKeyIndex rowKeyIndex) { int maxRounds = properties.getLlmRowMaxRounds(); @@ -1072,7 +1071,7 @@ public class AppearancePatentTaskService { List exhaustedKeys = exhausted.stream().map(this::rowKey).filter(key -> !key.isBlank()).toList(); boolean rowsTerminated = false; try { - mergeLlmRowsIntoChunks(task, allRowsByBaseId, llmClient.markRowsFailed(exhausted, reason), + mergeLlmRowsIntoChunks(task, llmClient.markRowsFailed(exhausted, reason), exhausted, rowKeyIndex); rowsTerminated = true; } finally { @@ -1087,7 +1086,6 @@ public class AppearancePatentTaskService { } private Set mergeLlmRowsIntoChunks(FileTaskEntity task, - Map> allRowsByBaseId, List llmRows, List batchRows, ChunkRowKeyIndex rowKeyIndex) { @@ -1108,8 +1106,7 @@ public class AppearancePatentTaskService { deduped.put(key, row); } } - return mergeLlmRowsIntoSubmittedChunks(task, new ArrayList<>(deduped.values()), allRowsByBaseId, - null, null, rowKeyIndex); + return mergeLlmRowsIntoSubmittedChunks(task, new ArrayList<>(deduped.values()), null, null, rowKeyIndex); } /** @@ -1128,31 +1125,63 @@ public class AppearancePatentTaskService { } + /** + * 收集未检测的候选行:**逐行**申领——同一父号下的 {@code 2_1/2_2/2_3} 每行都各自进候选、各自送检。 + * + *

历史上这里只挑「组代表行」,其余兄弟行指望 {@code expandRows} 在发起那一轮借用代表行的结论—— + * 那是把「同一个父号」当成「同一件东西」,而子序行各有各的 sku、标题和图片链接。 + * 口径(2026-10-10 用户定):这些行**都要检测**。折叠与借用一起撤掉,因为借用的后果不是省钱, + * 而是成片空行随结果文件交付(10-08 任务 30426:1211 行里只有 960 行有结论、251 行整段空白, + * 而任务报成功;10-09 任务 30455 的 281 行空行 100% 属于"同父号已有一行被判"这一形状)。 + * + *

"这行算不算已经检过"用交付口径(标题维+外观维+结论三列齐,见 + * {@link AppearancePatentRowOutcome#isJudged})。旧代码用的是"四个字段里任一可用",于是 + * 只带着专利维度文本、或只带一个 error 的行会被判"已检测",从此退出候选,而表格里它那三列全是空的。 + */ private List collectPendingLlmCandidates(List chunks, ChunkRowKeyIndex rowKeyIndex) { if (chunks == null || chunks.isEmpty()) { return List.of(); } List candidates = new ArrayList<>(); + int claimBlocked = 0; + int judgedSkipped = 0; + int noDataSkipped = 0; + int chunkRows = 0; for (TaskChunkEntity chunk : chunks) { Map persistedRows = readChunkRows(chunk); if (persistedRows.isEmpty()) { continue; } - // 这一遍已经把所有分片的载荷读进来了,顺手把「这片里有哪些行」记进索引: + // 这一遍已经把这批分片的载荷读进来了,顺手把「这片里有哪些行」记进索引: // 下面每批落库前都要这份索引,不记的话就得每批把全部分片重读重解析一遍 rememberChunkRowKeys(rowKeyIndex, chunk, persistedRows.keySet()); - for (AppearancePatentResultRowDto row : pickGroupRepresentativesForLlm(persistedRows.values())) { + chunkRows += persistedRows.size(); + for (AppearancePatentResultRowDto row : persistedRows.values()) { String key = rowKey(row); - if (key.isBlank() || hasResolvedLlmFields(row)) { + if (key.isBlank()) { + continue; + } + if (AppearancePatentRowOutcome.isJudged(row)) { + judgedSkipped++; + continue; + } + if (!shouldInspectRow(row)) { + // 连标题带图都没采到的行:送过去也只能回"外观识别异常",白扣一次 + noDataSkipped++; continue; } if (!taskCacheService.beginRowInspection(chunk.getTaskId(), key)) { + // 被"处理中/已完成"标记挡住的行必须数得出来:历史上这里静默 continue, + // 于是"扣了次数却永久没结论"和"这行本来就不该检"在日志里长得一模一样 + claimBlocked++; continue; } candidates.add(row); } } + log.info("[appearance-patent] 候选收集逐行申领 taskId={} chunkRows={} judgedSkipped={} noDataSkipped={} claimBlocked={} candidates={}", + chunks.get(0).getTaskId(), chunkRows, judgedSkipped, noDataSkipped, claimBlocked, candidates.size()); return candidates; } @@ -1167,8 +1196,8 @@ public class AppearancePatentTaskService { int total = 0; for (TaskChunkEntity chunk : chunks) { Map persistedRows = readChunkRows(chunk); - for (AppearancePatentResultRowDto row : pickGroupRepresentativesForLlm(persistedRows.values())) { - if (!rowKey(row).isBlank() && !hasResolvedLlmFields(row)) { + for (AppearancePatentResultRowDto row : persistedRows.values()) { + if (!rowKey(row).isBlank() && !AppearancePatentRowOutcome.isJudged(row)) { total++; } } @@ -1186,7 +1215,8 @@ public class AppearancePatentTaskService { if (persistedRows.isEmpty()) { continue; } - int unresolved = pickGroupRepresentativesForLlm(persistedRows.values()).size(); + int unresolved = (int) persistedRows.values().stream() + .filter(row -> !rowKey(row).isBlank() && !AppearancePatentRowOutcome.isJudged(row)).count(); if (unresolved > 0) { total += Math.max(1, (unresolved + batchSize - 1) / batchSize); } @@ -1226,8 +1256,7 @@ public class AppearancePatentTaskService { } log.info("[appearance-patent] stale recovery created assemble job taskId={} resultId={} jobId={} jobStatus={} chunkCount={}", task.getId(), result.getId(), job.getId(), job.getStatus(), chunks.size()); - Map> allRowsByBaseId = loadAllRowsByBaseId(task); - submitLlmBatches(task, result, job, chunks, allRowsByBaseId); + submitLlmBatches(task, result, job, chunks); if (isResultSubmissionComplete(task.getId())) { taskFileJobService.requeue(job.getId(), "检测结果已回流,正在组装 xlsx"); } else { @@ -1535,104 +1564,6 @@ public class AppearancePatentTaskService { } } - private List expandRows(List representatives, - Map> allRowsByBaseId) { - if (representatives == null || representatives.isEmpty()) { - return List.of(); - } - List result = new ArrayList<>(); - Set emittedKeys = new LinkedHashSet<>(); - for (AppearancePatentResultRowDto representative : representatives) { - List siblings = resolveSiblingRows(representative, allRowsByBaseId); - if (siblings == null || siblings.isEmpty()) { - result.add(representative); - continue; - } - for (AppearancePatentParsedRowVo sibling : siblings) { - String key = rowKey(sibling); - if (!emittedKeys.add(key)) { - continue; - } - result.add(copyResultToSibling(representative, sibling)); - } - } - return result; - } - - private List resolveSiblingRows(AppearancePatentResultRowDto representative, - Map> groupedRows) { - if (representative == null || groupedRows == null || groupedRows.isEmpty()) { - return List.of(); - } - String representativeGroupKey = normalize(representative.getGroupKey()); - if (!representativeGroupKey.isBlank()) { - return groupedRows.getOrDefault(representativeGroupKey, List.of()); - } - String representativeKey = rowKey(representative); - String representativeLegacyKey = legacyRowKey(representative); - for (List rows : groupedRows.values()) { - for (AppearancePatentParsedRowVo row : rows) { - if (Objects.equals(representativeKey, rowKey(row)) - || Objects.equals(representativeLegacyKey, legacyRowKey(row))) { - return rows; - } - } - } - return List.of(); - } - - private Map> loadAllRowsByBaseId(FileTaskEntity task) { - try { - AppearancePatentParsedPayloadDto payload = readParsedPayload(task); - List rows = payload.getAllItems(); - if (rows == null || rows.isEmpty()) { - rows = payload.getItems(); - } - if ((rows == null || rows.isEmpty()) && payload.getGroups() != null) { - rows = payload.getGroups().stream() - .filter(Objects::nonNull) - .flatMap(group -> group.getItems() == null ? java.util.stream.Stream.empty() : group.getItems().stream()) - .toList(); - } - return groupRowsByBaseId(rows); - } catch (Exception ex) { - log.warn("[appearance-patent] read all rows failed taskId={} err={}", task.getId(), ex.getMessage()); - return new LinkedHashMap<>(); - } - } - - private List pickGroupRepresentativesForLlm(java.util.Collection rows) { - Map> groupedRows = new LinkedHashMap<>(); - if (rows == null) { - return List.of(); - } - for (AppearancePatentResultRowDto row : rows) { - if (row == null) { - continue; - } - String key = firstNonBlank(normalize(row.getGroupKey()), rowKey(row)); - groupedRows.computeIfAbsent(key, ignored -> new ArrayList<>()).add(row); - } - List representatives = new ArrayList<>(); - for (List siblings : groupedRows.values()) { - boolean alreadyResolved = siblings.stream().anyMatch(this::hasCompleteLlmResult); - if (alreadyResolved) { - continue; - } - AppearancePatentResultRowDto candidate = null; - for (AppearancePatentResultRowDto sibling : siblings) { - if (shouldInspectRow(sibling)) { - candidate = sibling; - break; - } - } - if (candidate != null) { - representatives.add(candidate); - } - } - return representatives; - } - private List buildParsedGroups(List rows) { List groups = new ArrayList<>(); for (List siblings : groupRowsByBaseId(rows).values()) { @@ -1724,33 +1655,6 @@ public class AppearancePatentTaskService { } } - private AppearancePatentResultRowDto copyResultToSibling(AppearancePatentResultRowDto representative, AppearancePatentParsedRowVo sibling) { - AppearancePatentResultRowDto row = new AppearancePatentResultRowDto(); - row.setSourceFileKey(sibling.getSourceFileKey()); - row.setSourceFilename(sibling.getSourceFilename()); - row.setRowToken(sibling.getRowToken()); - row.setGroupKey(sibling.getGroupKey()); - row.setId(sibling.getDisplayId()); - row.setAsin(sibling.getAsin()); - row.setCountry(sibling.getCountry()); - row.setSku(firstNonBlank(representative.getSku(), sibling.getSku())); - row.setBrand(representative.getBrand()); - row.setUrl(firstNonBlank(representative.getUrl(), sibling.getUrl())); - row.setTitle(firstNonBlank(representative.getTitle(), sibling.getTitle())); - row.setError(representative.getError()); - row.setStatus(representative.getStatus()); - row.setTitleRisk(representative.getTitleRisk()); - row.setAppearanceRisk(representative.getAppearanceRisk()); - row.setPatentRisk(representative.getPatentRisk()); - row.setConclusion(representative.getConclusion()); - row.setTitleReason(representative.getTitleReason()); - row.setAppearanceReason(representative.getAppearanceReason()); - row.setPatentReason(representative.getPatentReason()); - row.setScore(representative.getScore()); - row.setDone(representative.getDone()); - return row; - } - private String readAiPrompt(FileTaskEntity task) { try { return readParsedPayload(task).getAiPrompt(); @@ -1781,6 +1685,7 @@ public class AppearancePatentTaskService { private void finalizeTask(FileTaskEntity task, String error, int rowCount, boolean assembleWorkbook) { String finalError = error; + AppearancePatentRowOutcome.Facts deliveryFacts = AppearancePatentRowOutcome.Facts.EMPTY; FileResultEntity result = null; boolean hasPersistedResultRows = hasPersistedResultRows(task.getId()); List rows = fileResultMapper.selectList(new LambdaQueryWrapper() @@ -1796,7 +1701,7 @@ public class AppearancePatentTaskService { boolean shouldAssembleResult = assembleWorkbook && (finalError == null || finalError.isBlank() || hasPersistedResultRows); if (shouldAssembleResult && shouldAssembleSynchronously()) { try { - assembleResultWorkbook(task, result); + deliveryFacts = assembleResultWorkbook(task, result); } catch (Exception ex) { log.warn("[appearance-patent] assemble result workbook failed taskId={} err={}", task.getId(), ex.getMessage()); finalError = firstNonBlank(finalError, "Generate appearance patent result failed"); @@ -1808,7 +1713,8 @@ public class AppearancePatentTaskService { task.setStatus(resolveTaskExecutionStatus(waitingForAssemble, failed)); task.setSuccessFileCount(failed ? 0 : 1); task.setFailedFileCount(failed ? 1 : 0); - task.setErrorMessage(finalError); + // 缺口只写在消息里,不参与 failed 计算:95% 以上有结论的单仍是成功,但用户要看得见"N 行没检测完" + task.setErrorMessage(appendDeliveryGapMessage(finalError, deliveryFacts)); task.setUpdatedAt(LocalDateTime.now()); task.setFinishedAt(waitingForAssemble ? null : LocalDateTime.now()); fileTaskMapper.updateById(task); @@ -1921,25 +1827,58 @@ public class AppearancePatentTaskService { int plannedLlmUnits = Math.max(1, llmWorkUnits); int totalProgressUnits = Math.max(3, plannedLlmUnits + 3); saveFileBuildProgress(task, job, totalProgressUnits, 0, "正在提交检测"); - Map> allRowsByBaseId = loadAllRowsByBaseId(task); - submitLlmBatches(task, result, job, chunks, allRowsByBaseId); + submitLlmBatches(task, result, job, chunks); if (STATUS_RUNNING.equals(task.getStatus()) && !isResultSubmissionComplete(task.getId())) { taskFileJobService.touchRunning(job.getId()); touchJavaSideTaskActivity(task.getId()); saveFileBuildProgress(task, job, totalProgressUnits, Math.max(1, plannedLlmUnits), "等待 Python 继续回传数据"); return false; } - completeLlmFileJob(task, result, job, totalProgressUnits); + return completeLlmFileJob(task, result, job, totalProgressUnits); + } + + /** + * 有行没跑完就先别把半张表当成品发:撤销这些行的检测标记(完成态与处理中都要撤, + * 否则 {@code beginRowInspection} 下一轮仍然申领不到它们),把作业排回队列补检一轮。 + * + *

不会无限补检:作业重排受 {@code TaskFileJobService} 的 MAX_RETRY_COUNT 约束(requeue 返回 + * false 时这里就放行出文件),行级重发次数受 {@code llmRowMaxRounds} 约束(到顶的行会被判死并 + * 写上「重发轮次已达上限」的结论,从此不再是空行)。 + */ + private boolean requestRecheckForMissingRows(FileTaskEntity task, TaskFileJobEntity job, + AppearancePatentRowOutcome.Facts facts) { + if (task == null || job == null || job.getId() == null || facts == null + || facts.missingRows() <= 0 || facts.missingKeys().isEmpty()) { + return false; + } + List keys = facts.missingKeys(); + // 只有这条出口允许连完成标记一起撤:结果落过库却又读不回来的行,不撤就永远补不回来。 + // 代价(该行可能被重发一次 LLM、多扣一次次数)是这一档修法里用户点名要付的。 + taskCacheService.releaseRowInspection(task.getId(), keys, true); + boolean requeued = taskFileJobService.requeue(job.getId(), + "有 " + facts.missingRows() + " 行未完成检测,补检一轮后重新组装结果文件"); + if (!requeued) { + log.warn("[appearance-patent] 交付缺口补检排不上队,按已有结果交付 taskId={} jobId={} missingRows={}", + task.getId(), job.getId(), facts.missingRows()); + return false; + } + log.warn("[appearance-patent] 交付缺口触发补检轮 taskId={} jobId={} missingRows={} keys={} noDataRows={}", + task.getId(), job.getId(), facts.missingRows(), keys.size(), facts.noDataRows()); return true; } - private void completeLlmFileJob(FileTaskEntity task, + private boolean completeLlmFileJob(FileTaskEntity task, FileResultEntity result, TaskFileJobEntity job, int totalProgressUnits) { int assembleProgress = Math.max(1, totalProgressUnits - 2); saveFileBuildProgress(task, job, totalProgressUnits, assembleProgress, "正在组装 xlsx"); - assembleResultWorkbook(task, result); + AppearancePatentRowOutcome.Facts deliveryFacts = assembleResultWorkbook(task, result); + if (requestRecheckForMissingRows(task, job, deliveryFacts)) { + // 半张表先不发出去:撤销未完成行的检测标记、把作业排回队列补检一轮, + // 上限由行级轮次闸(llmRowMaxRounds)与作业重试上限共同兜住。 + return false; + } saveFileBuildProgress(task, job, totalProgressUnits, totalProgressUnits - 1, "正在上传结果文件"); fileResultMapper.updateById(result); boolean taskAlreadyFailed = STATUS_FAILED.equals(task.getStatus()) @@ -1948,18 +1887,18 @@ public class AppearancePatentTaskService { task.setStatus(resolveTaskExecutionStatus(false, taskAlreadyFailed)); task.setSuccessFileCount(taskAlreadyFailed ? 0 : 1); task.setFailedFileCount(taskAlreadyFailed ? 1 : 0); - task.setErrorMessage(taskAlreadyFailed ? existingError : null); + task.setErrorMessage(appendDeliveryGapMessage(taskAlreadyFailed ? existingError : null, deliveryFacts)); task.setUpdatedAt(LocalDateTime.now()); task.setFinishedAt(LocalDateTime.now()); fileTaskMapper.updateById(task); taskCacheService.deleteTaskCache(task.getId()); saveFileBuildProgress(task, job, totalProgressUnits, totalProgressUnits, "结果文件已生成"); + return true; } /** 返回真正落库的行键;没匹配到分片行的键不会出现在里面,调用方据此决定要不要撤销已检测标记。 */ private Set mergeLlmRowsIntoSubmittedChunks(FileTaskEntity task, List llmRows, - Map> allRowsByBaseId, String fallbackScopeHash, Integer fallbackChunkIndex, ChunkRowKeyIndex rowKeyIndex) { @@ -1979,7 +1918,7 @@ public class AppearancePatentTaskService { } Map> mergeRowsByChunk = new LinkedHashMap<>(); for (AppearancePatentResultRowDto resultRow : llmRows) { - for (AppearancePatentResultRowDto expandedRow : expandRows(List.of(resultRow), allRowsByBaseId)) { + for (AppearancePatentResultRowDto expandedRow : List.of(resultRow)) { String rowKey = rowKey(expandedRow); if (rowKey.isBlank()) { continue; @@ -2256,7 +2195,7 @@ public class AppearancePatentTaskService { .eq(TaskScopeStateEntity::getModuleType, MODULE_TYPE)); } - private void assembleResultWorkbook(FileTaskEntity task, FileResultEntity result) { + private AppearancePatentRowOutcome.Facts assembleResultWorkbook(FileTaskEntity task, FileResultEntity result) { AppearancePatentParsedPayloadDto parsed = readParsedPayload(task); List parsedRows = parsed.getAllItems() == null ? List.of() : parsed.getAllItems(); @@ -2268,6 +2207,7 @@ public class AppearancePatentTaskService { long resolvedRows = 0L; long reasonRows = 0L; int conclusionPropagated = 0; + AppearancePatentRowOutcome.Facts deliveryFacts = AppearancePatentRowOutcome.Facts.EMPTY; File outputDir = new File(storageProperties.getLocalTempDir(), "appearance-patent-result"); if (!outputDir.exists() && !outputDir.mkdirs()) { throw new BusinessException("创建结果目录失败"); @@ -2280,7 +2220,8 @@ public class AppearancePatentTaskService { loadPersistedResultRowsForRowsWithRetry(task.getId(), item.rows()); // 覆盖率先于「本文件无结果就跳过」判定:整源文件的 LLM 结果全丢(分片被跳过、 // 结果清空)时覆盖率是 0%,必须在这里判掉,不能让 zip 少一个文件还报任务成功。 - validateCompleteLlmCoverage(task.getId(), item.rows(), resultMap); + deliveryFacts = deliveryFacts.plus( + validateCompleteLlmCoverage(task.getId(), item.rows(), resultMap)); List receivedRows = filterReceivedParsedRows(item.rows(), resultMap); if (receivedRows.isEmpty()) { resultMap.clear(); @@ -2351,6 +2292,7 @@ public class AppearancePatentTaskService { result.setResultFileSize(uploadFile.length()); result.setResultContentType(contentType); result.setRowCount(receivedRowCount); + return deliveryFacts; } finally { for (SourceResultWorkbook workbook : workbooks) { if (workbook.file().exists() && !workbook.file().delete()) { @@ -2474,68 +2416,90 @@ public class AppearancePatentTaskService { } /** - * 覆盖率判定:分母必须是**解析出的全部行**(只滤掉不需要送检的行),不能是已经拿到结果的行。 - * 传已过滤的集合会让分母恒等于分子 —— 整片结果丢失(chunk payload 缺失被跳过、 - * 客户端漏回传)时覆盖率仍是 100%,阈值形同虚设。 + * 覆盖率判定:分母必须是**真正会写进结果文件的那批行**,且"该行有没有数据"要同时看 + * 用户上传表的解析行与客户端回传的结果行——只看解析行,用户表不带标题列时分母恒为 0, + * 闸门对整份文件失明(现网 6 份已交付文件里 20%–51% 空行、零告警就是这么来的)。 + * 判据本身在 {@link AppearancePatentRowOutcome#summarize},与表格文案共用一套,不许两边各判。 */ - private void validateCompleteLlmCoverage(Long taskId, + private AppearancePatentRowOutcome.Facts validateCompleteLlmCoverage(Long taskId, List parsedRows, Map resultMap) { if (parsedRows == null || parsedRows.isEmpty()) { - return; + return AppearancePatentRowOutcome.Facts.EMPTY; } + AppearancePatentRowOutcome.Facts facts = + AppearancePatentRowOutcome.summarize(parsedRows, row -> findResultRow(row, resultMap)); FileTaskEntity task = fileTaskMapper.selectById(taskId); - if (task != null && STATUS_FAILED.equals(task.getStatus())) { - return; + boolean taskAlreadyFailed = task != null && STATUS_FAILED.equals(task.getStatus()); + if (facts.missingRows() > 0 || facts.noDataRows() > 0) { + log.warn("[appearance-patent] 交付行账不完整 taskId={} expectedRows={} judgedRows={} missingRows={} noDataRows={} coverage={} taskFailed={} samples={}", + taskId, facts.expectedRows(), facts.judgedRows(), facts.missingRows(), facts.noDataRows(), + String.format("%.2f%%", facts.coverage() * 100), taskAlreadyFailed, + sampleAsins(parsedRows, resultMap, facts)); + } + double minCoverage = properties.getLlmCoverageMinRatio(); + // 已判失败的任务走的是"按已有分片出部分结果"这条既有口径,这里再抛会把成品彻底拿掉; + // 但它不再静默:上面的行账锚点必打,缺口由调用方写进任务错误信息,用户看得到「N 行没检测」。 + if (!taskAlreadyFailed && minCoverage > 0 && facts.expectedRows() > 0 + && facts.coverage() + 1e-9 < minCoverage) { + throw new BusinessException("LLM 结果不完整:仅 " + facts.judgedRows() + "/" + facts.expectedRows() + + " 行拿到检测结论(低于 " + Math.round(minCoverage * 100) + "% 覆盖率下限)" + + noDataSuffix(facts) + ",请重新运行任务"); + } + return facts; + } + + private List sampleAsins(List parsedRows, + Map resultMap, + AppearancePatentRowOutcome.Facts facts) { + List samples = new ArrayList<>(5); + if (facts.missingRows() <= 0) { + return samples; } - int expectedRows = 0; - int missingRows = 0; - List sampleAsins = new ArrayList<>(); for (AppearancePatentParsedRowVo parsedRow : parsedRows) { - if (!hasPromptFields(parsedRow)) { + if (samples.size() >= 5) { + break; + } + if (parsedRow == null) { continue; } - expectedRows++; - AppearancePatentResultRowDto resultRow = findResultRow(parsedRow, resultMap); - if (resultRow == null || !hasDeliverableLlmResult(resultRow)) { - missingRows++; - if (sampleAsins.size() < 5) { - sampleAsins.add(firstNonBlank(parsedRow.getAsin(), firstNonBlank(parsedRow.getDisplayId(), ""))); - } + if (AppearancePatentRowOutcome.classify(parsedRow, findResultRow(parsedRow, resultMap)) + != AppearancePatentRowOutcome.Kind.NOT_INSPECTED) { + continue; } + samples.add(firstNonBlank(parsedRow.getAsin(), + firstNonBlank(parsedRow.getDisplayId(), ""))); } - if (expectedRows <= 0) { - return; - } - int resolvedRows = expectedRows - missingRows; - double coverage = (double) resolvedRows / expectedRows; - double minCoverage = properties.getLlmCoverageMinRatio(); - if (missingRows > 0) { - log.warn("[appearance-patent] incomplete llm coverage taskId={} expectedRows={} missingRows={} coverage={} samples={}", - taskId, expectedRows, missingRows, String.format("%.2f%%", coverage * 100), sampleAsins); - } - if (minCoverage > 0 && coverage + 1e-9 < minCoverage) { - throw new BusinessException("LLM 结果不完整:仅 " + resolvedRows + "/" + expectedRows - + " 行拿到检测结论(低于 " + Math.round(minCoverage * 100) + "% 覆盖率下限),请重新运行任务"); - } + return samples; } - /** - * 交付口径的完整性判定:结论依赖标题维度、外观维度和最终结论三列。 - * 与 {@link #hasCompleteLlmResult} 的区别是不看 patentRisk —— 现网正常返回里 patent - * 恒为空串(见 AppearancePatentLlmClient 的 LlmResult 构造),拿它当必要条件会把每个任务都判成不完整。 - */ - private boolean hasDeliverableLlmResult(AppearancePatentResultRowDto row) { - return hasUsableLlmField(row.getTitleRisk()) - && hasUsableLlmField(row.getAppearanceRisk()) - && hasUsableLlmField(row.getConclusion()); + private String noDataSuffix(AppearancePatentRowOutcome.Facts facts) { + return facts.noDataRows() > 0 + ? ";另有 " + facts.noDataRows() + " 行未采集到数据、无法检测" + : ""; } - private boolean hasCompleteLlmResult(AppearancePatentResultRowDto row) { - return hasUsableLlmField(row.getTitleRisk()) - && hasUsableLlmField(row.getAppearanceRisk()) - && hasUsableLlmField(row.getPatentRisk()) - && hasUsableLlmField(row.getConclusion()); + /** 交付缺口写进任务错误信息:让后台列表与用户端都能看到「N 行没检测完」,而不是只有一张半空表。 */ + private String appendDeliveryGapMessage(String existingError, AppearancePatentRowOutcome.Facts facts) { + if (facts == null || (facts.missingRows() <= 0 && facts.noDataRows() <= 0)) { + return existingError; + } + StringBuilder gap = new StringBuilder(); + if (facts.missingRows() > 0) { + gap.append("有 ").append(facts.missingRows()).append(" 行未完成检测"); + } + if (facts.noDataRows() > 0) { + if (gap.length() > 0) { + gap.append("、"); + } + gap.append("有 ").append(facts.noDataRows()).append(" 行未采集到数据"); + } + gap.append("(结果文件中这些行已单独标注)"); + String existing = firstNonBlank(existingError, ""); + if (existing.contains("未完成检测") || existing.contains("未采集到数据")) { + return existing; + } + return existing.isBlank() ? gap.toString() : existing + ";" + gap; } private boolean hasPersistedResultRows(Long taskId) { @@ -2853,29 +2817,10 @@ public class AppearancePatentTaskService { return false; } - private boolean hasPromptFields(AppearancePatentParsedRowVo row) { - return row != null && (!normalize(row.getTitle()).isBlank() || !normalize(row.getUrl()).isBlank()); - } - private boolean shouldInspectRow(AppearancePatentResultRowDto row) { return row != null && (!normalize(row.getTitle()).isBlank() || !normalize(row.getUrl()).isBlank()); } - private boolean hasResolvedLlmFields(AppearancePatentResultRowDto row) { - if (row == null) { - return false; - } - return hasUsableLlmField(row.getTitleRisk()) - || hasUsableLlmField(row.getAppearanceRisk()) - || hasUsableLlmField(row.getPatentRisk()) - || hasUsableLlmField(row.getConclusion()); - } - - private boolean hasUsableLlmField(String value) { - String normalized = normalize(value); - return !normalized.isBlank() && !AppearancePatentSheetBuilder.isTechnicalLlmFailure(normalized); - } - private Map buildHeaderMap(Row header, DataFormatter formatter) { Map map = new LinkedHashMap<>(); for (int i = 0; i < header.getLastCellNum(); i++) { diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentSheetBuilder.java b/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentSheetBuilder.java index d32632cdc..02e9334da 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentSheetBuilder.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentSheetBuilder.java @@ -43,9 +43,10 @@ public final class AppearancePatentSheetBuilder { } /** - * 没拿到判定的行写进格子里的显式文案。 + * 「检到了这一行、但这一列没给出文本」时写进格子的文案。 * - *

交付只要求 LLM 覆盖率过半({@code llm-coverage-min-ratio}),所以少数缺失行照样出文件。 + *

整行压根没检出结论的,走 {@link AppearancePatentRowOutcome#NOT_INSPECTED_TEXT} 与 + * {@link AppearancePatentRowOutcome#NO_DATA_TEXT}——三种原因对应的处置动作不同,不能挤进同一句。 * 历史上这些格子留空,而"空白"和"没风险"在表格里长得一模一样——用户把漏检当成了安全, * 这正是最贵的那种静默失败。 */ @@ -58,7 +59,11 @@ public final class AppearancePatentSheetBuilder { */ public static final String PENDING_REVIEW_SUFFIX = "·商标待复核"; - private static final String NO_RESULT_REASON = NO_RESULT_TEXT + ":该行没有返回判定,重新运行任务可补检"; + private static final String NOT_INSPECTED_REASON = AppearancePatentRowOutcome.NOT_INSPECTED_TEXT + + ":该行没拿到判定,重新运行任务可补检"; + + private static final String NO_DATA_REASON = AppearancePatentRowOutcome.NO_DATA_TEXT + + ":请先在采集侧补齐该行数据"; public static void buildResultSheet(Workbook workbook, List rows, Map resultMap) { @@ -94,6 +99,8 @@ public final class AppearancePatentSheetBuilder { continue; } AppearancePatentResultRowDto resultRow = finder.apply(parsedRow); + AppearancePatentRowOutcome.Kind kind = AppearancePatentRowOutcome.classify(parsedRow, resultRow); + String outcomeMissingText = AppearancePatentRowOutcome.missingText(kind); Row row = sheet.createRow(rowIndex++); int col = 0; row.createCell(col++).setCellValue(firstNonBlank(parsedRow.getDisplayId(), parsedRow.getSourceId())); @@ -102,17 +109,18 @@ public final class AppearancePatentSheetBuilder { row.createCell(col++).setCellValue(readValueByHeader(parsedRow, "卖家名称", "卖家名", "卖家", "店铺名称", "店铺名", "seller name", "seller_name", "seller-name", "sellername", "store name", "shop name")); row.createCell(col++).setCellValue(resolveBrand(resultRow, parsedRow)); row.createCell(col++).setCellValue(resolvePrice(resultRow)); - row.createCell(col++).setCellValue(resultRow == null ? firstNonBlank(parsedRow.getTitle(), "") : firstNonBlank(resultRow.getTitle(), parsedRow.getTitle())); - row.createCell(col++).setCellValue(resultRow == null ? firstNonBlank(parsedRow.getUrl(), "") : firstNonBlank(resultRow.getUrl(), parsedRow.getUrl())); + row.createCell(col++).setCellValue(firstNonBlank(AppearancePatentRowOutcome.titleOf(parsedRow, resultRow), "")); + row.createCell(col++).setCellValue(firstNonBlank(AppearancePatentRowOutcome.urlOf(parsedRow, resultRow), "")); row.createCell(col++).setCellValue(resultRow == null ? firstNonBlank(parsedRow.getSku(), "") : firstNonBlank(resultRow.getSku(), parsedRow.getSku())); - String titleRiskCell = resultRow == null - ? NO_RESULT_TEXT + // 行级文案只在"整行什么都没带回来"时接管:带着 LLM 错误原文的行必须照原样显示错误, + // 否则用户和运维都失去归因线索(这条是老用例 test_sheet_builder_llm_failure_user_facing_cells 撞出来的) + boolean rowLevelText = kind != AppearancePatentRowOutcome.Kind.JUDGED + && AppearancePatentRowOutcome.carriesNothingToShow(resultRow); + String titleRiskCell = rowLevelText ? outcomeMissingText : firstNonBlank(userFacingLlmCellValue(resultRow, resultRow.getTitleRisk()), NO_RESULT_TEXT); - String appearanceRiskCell = resultRow == null - ? NO_RESULT_TEXT + String appearanceRiskCell = rowLevelText ? outcomeMissingText : firstNonBlank(userFacingLlmCellValue(resultRow, resultRow.getAppearanceRisk()), NO_RESULT_TEXT); - String conclusionCell = resultRow == null - ? NO_RESULT_TEXT + String conclusionCell = rowLevelText ? outcomeMissingText : firstNonBlank(userFacingConclusion(resultRow), NO_RESULT_TEXT); row.createCell(col++).setCellValue(titleRiskCell); row.createCell(col++).setCellValue(appearanceRiskCell); @@ -148,15 +156,24 @@ public final class AppearancePatentSheetBuilder { AppearancePatentResultRowDto resultRow = finder.apply(parsedRow); Row row = sheet.createRow(rowIndex++); row.createCell(0).setCellValue(asin); - // 只有"整行没结果"才代写原因;有结果的行原本就没原因可写(无风险),不能污染成缺失说明 - String missingReason = resultRow == null ? NO_RESULT_REASON : ""; - row.createCell(1).setCellValue(resultRow == null ? missingReason : firstNonBlank(resultRow.getAppearanceReason(), "")); - row.createCell(2).setCellValue(resultRow == null ? missingReason : firstNonBlank(resultRow.getPatentReason(), "")); - row.createCell(3).setCellValue(resultRow == null ? missingReason : firstNonBlank(resultRow.getTitleReason(), "")); + // 只有"这行没给出结论"才代写原因;已判定的行原本就没原因可写(无风险),不能污染成缺失说明 + String missingReason = missingReason(AppearancePatentRowOutcome.classify(parsedRow, resultRow)); + row.createCell(1).setCellValue(resultRow == null ? missingReason : firstNonBlank(resultRow.getAppearanceReason(), missingReason)); + row.createCell(2).setCellValue(resultRow == null ? missingReason : firstNonBlank(resultRow.getPatentReason(), missingReason)); + row.createCell(3).setCellValue(resultRow == null ? missingReason : firstNonBlank(resultRow.getTitleReason(), missingReason)); rowIndex++; } } + /** 结论没出来时,「原因」sheet 要说清是哪一种没出来:没数据 / 没跑到 / 跑了没答上。 */ + private static String missingReason(AppearancePatentRowOutcome.Kind kind) { + return switch (kind) { + case NO_DATA -> NO_DATA_REASON; + case NOT_INSPECTED -> NOT_INSPECTED_REASON; + case JUDGED -> ""; + }; + } + public static AppearancePatentResultRowDto findResultRow(AppearancePatentParsedRowVo parsedRow, Map resultMap) { if (parsedRow == null || resultMap == null || resultMap.isEmpty()) { @@ -298,7 +315,7 @@ public final class AppearancePatentSheetBuilder { return preferred == null || preferred.isBlank() ? fallback : preferred.trim(); } - private static String normalize(String val) { + public static String normalize(String val) { return val == null ? "" : val.replace(String.valueOf((char) 0xFEFF), "").replace((char) 0x3000, ' ').trim().replaceAll("\\s+", " "); } } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/similarasin/service/support/SimilarAsinResultTextSupport.java b/backend-java/src/main/java/com/nanri/aiimage/modules/similarasin/service/support/SimilarAsinResultTextSupport.java index b7c3b100d..8bf77d4ac 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/similarasin/service/support/SimilarAsinResultTextSupport.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/similarasin/service/support/SimilarAsinResultTextSupport.java @@ -94,6 +94,17 @@ public final class SimilarAsinResultTextSupport { return !normalized.isBlank() && !isTechnicalLlmFailure(normalized); } + /** + * 交付口径:结果表真正给用户的两个判定列(相似度、是否同款)都得有可用文本,才算这行给出了结论。 + * 与 {@link #hasResolvedLlmFields} 的区别是后者是「十一个字段任一可用」——只填上 category 或 status + * 的行会被读成「已经检测」,而它在表格里两列都是空的。这条先只做度量入口,不改既有行为。 + */ + public static boolean hasDeliverableResult(SimilarAsinResultRowDto row) { + return row != null + && hasUsableLlmField(row.getSimilarity()) + && hasUsableLlmField(row.getIsConform()); + } + public static boolean hasResolvedLlmFields(SimilarAsinResultRowDto row) { if (row == null) { return false; diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/similarasin/service/support/SimilarAsinResultWorkbookAssembler.java b/backend-java/src/main/java/com/nanri/aiimage/modules/similarasin/service/support/SimilarAsinResultWorkbookAssembler.java index 522cd39de..bce56a1ed 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/similarasin/service/support/SimilarAsinResultWorkbookAssembler.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/similarasin/service/support/SimilarAsinResultWorkbookAssembler.java @@ -189,11 +189,16 @@ public class SimilarAsinResultWorkbookAssembler { List.of("不符合"), "[similar-asin] taskId=" + task.getId() ); + long undeliveredRows = captured.rows().stream() + .map(row -> SimilarAsinChunkMergeSupport.findResultRow(row, sourceResultMap)) + .filter(java.util.Objects::nonNull) + .filter(resultRow -> !SimilarAsinResultTextSupport.hasDeliverableResult(resultRow)) + .count(); writeResultWorkbook(xlsx, captured.rows(), sourceResultMap, taskImageCache, imageSpool); ensureResultAssemblyNotInterrupted(); return new SourceResultWorkbook( xlsx, filename, captured.rows().size(), persistedRows, - sourceResultMap.size(), resolvedRows, conformPropagated); + sourceResultMap.size(), resolvedRows, conformPropagated, undeliveredRows); })); } try { @@ -235,9 +240,18 @@ public class SimilarAsinResultWorkbookAssembler { int conformPropagated = workbooks.stream() .mapToInt(SourceResultWorkbook::conformPropagated) .sum(); - log.info("[similar-asin] bounded assemble workbook taskId={} parsedRows={} persistedRows={} resultRows={} resolvedRows={} propagated={} sources={} costMs={}", + long undeliveredRows = workbooks.stream() + .mapToLong(SourceResultWorkbook::undeliveredRows) + .sum(); + if (undeliveredRows > 0) { + // 交付行账:货源这一面还没有覆盖率闸,先把「表格里两列空着的行」数出来并报警, + // 有了真实分布才谈得上定阈值(不拿别的模块的数字当这里的基线) + log.warn("[similar-asin] 交付行账不完整 taskId={} resultRows={} undeliveredRows={} sources={}", + task.getId(), resultRows, undeliveredRows, sourceRows.size()); + } + log.info("[similar-asin] bounded assemble workbook taskId={} parsedRows={} persistedRows={} resultRows={} resolvedRows={} propagated={} undeliveredRows={} sources={} costMs={}", task.getId(), parsedRows.size(), persistedResultRows, resultRows, - resolvedRows, conformPropagated, sourceRows.size(), + resolvedRows, conformPropagated, undeliveredRows, sourceRows.size(), System.currentTimeMillis() - assembleStart); if (resultRows == 0) { String failureSummary = chunkPayloadSupport.collectChunkReadFailureSummary(task.getId()); @@ -1084,9 +1098,10 @@ public class SimilarAsinResultWorkbookAssembler { int persistedRows, int resultRows, long resolvedRows, - int conformPropagated) { + int conformPropagated, + long undeliveredRows) { private SourceResultWorkbook(File file, String filename, int rowCount) { - this(file, filename, rowCount, 0, 0, 0L, 0); + this(file, filename, rowCount, 0, 0, 0L, 0, 0L); } } diff --git a/backend-java/src/main/resources/application.yml b/backend-java/src/main/resources/application.yml index 3aa5318d8..23be7a055 100644 --- a/backend-java/src/main/resources/application.yml +++ b/backend-java/src/main/resources/application.yml @@ -381,8 +381,9 @@ aiimage: llm-row-concurrency: ${AIIMAGE_APPEARANCE_PATENT_LLM_ROW_CONCURRENCY:5} max-parse-rows: ${AIIMAGE_APPEARANCE_PATENT_MAX_PARSE_ROWS:50000} # LLM 覆盖率下限:组装结果时拿到结论的行占比低于该值即判任务失败(<=0 关闭)。 - # 正常任务实测约 0.91,默认 0.5 只拦"904 行只检 1 行"这类塌陷。 - llm-coverage-min-ratio: ${AIIMAGE_APPEARANCE_PATENT_LLM_COVERAGE_MIN_RATIO:0.5} + # 2026-10-10 由 0.5 抬到 0.95:0.5 等于允许半张表空着出文件(现网 10-06~10-09 六份成品 + # 实测空行 20.7%~51.3%,全部放行且零告警,因为旧分母按解析行标题判、用户表不带标题列时分母为 0)。 + llm-coverage-min-ratio: ${AIIMAGE_APPEARANCE_PATENT_LLM_COVERAGE_MIN_RATIO:0.95} llm-retry-times: ${AIIMAGE_APPEARANCE_PATENT_LLM_RETRY_TIMES:3} # 单行最多被重发几轮(<=0 关闭)。失败行会被释放、下一轮重新申领,没有这道闸时 # 上游劣化窗口内会无限重发并每轮扣用户 LLM 次数——deferRunning 不累加 retryCount,任务层那道永不触发。 diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentChunkRowIndexTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentChunkRowIndexTest.java index 375305eab..e7c8a29cb 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentChunkRowIndexTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentChunkRowIndexTest.java @@ -200,8 +200,7 @@ class AppearancePatentChunkRowIndexTest { ReflectionTestUtils.invokeMethod(service, "submitLlmBatches", task(), new FileResultEntity(), job(), List.of(chunk(SCOPE_A, POINTER_A, HASH_A), chunk(SCOPE_B, POINTER_B, HASH_B), - chunk(SCOPE_C, POINTER_C, "hash-c-stale")), - Map.of()); + chunk(SCOPE_C, POINTER_C, "hash-c-stale"))); verify(transientPayloadStorageService, times(2)).resolvePayload(eq(POINTER_C), anyString()); } @@ -212,8 +211,7 @@ class AppearancePatentChunkRowIndexTest { ReflectionTestUtils.invokeMethod(service, "submitLlmBatches", task(), new FileResultEntity(), job(), List.of(chunk(SCOPE_A, POINTER_A, HASH_A), chunk(SCOPE_B, POINTER_B, HASH_B), - chunk(SCOPE_C, POINTER_C, HASH_C)), - Map.of()); + chunk(SCOPE_C, POINTER_C, HASH_C))); } private FileTaskEntity task() { diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentRowMarkTimingTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentRowMarkTimingTest.java index 9281c25fa..041ba46f0 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentRowMarkTimingTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentRowMarkTimingTest.java @@ -47,6 +47,9 @@ import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.contains; @@ -317,7 +320,7 @@ class AppearancePatentRowMarkTimingTest { private void submit() { ReflectionTestUtils.invokeMethod(service, "submitLlmBatches", - task(), new FileResultEntity(), job(), List.of(chunk()), Map.of()); + task(), new FileResultEntity(), job(), List.of(chunk())); } private void stubMergeWrite(int updatedRows) { @@ -347,6 +350,118 @@ class AppearancePatentRowMarkTimingTest { return job; } + /** + * 只带专利维度文本的行必须仍然是候选。旧口径把"四个字段任一可用"当成已检测, + * 而这种行会在交付表格里显示成三列全空(标题维/外观维/结论)——退出候选就等于永久空白。 + */ + @Test + @SuppressWarnings("unchecked") + void rowCarryingOnlyPatentRiskIsStillACandidate() { + String onlyPatent = "[{\"rowToken\":\"r3\",\"asin\":\"B0A0000003\",\"title\":\"BAG\"," + + "\"url\":\"http://img/3.jpg\",\"groupKey\":\"g3\",\"patentRisk\":\"无侵权\"}]"; + when(transientPayloadStorageService.resolvePayload(eq(POINTER), anyString())).thenReturn(onlyPatent); + when(taskCacheService.beginRowInspection(eq(TASK_ID), anyString())).thenReturn(true); + + List candidates = (List) ReflectionTestUtils + .invokeMethod(service, "collectPendingLlmCandidates", List.of(chunk()), new AppearancePatentTaskService.ChunkRowKeyIndex()); + + assertEquals(1, candidates.size()); + assertEquals("r3", candidates.get(0).getRowToken()); + } + + /** + * 同一父号下的子序行必须各自行级申领(2026-10-10 口径):{@code 2_1/2_2/2_3} 是不同 sku、 + * 各有各的标题与图片链接,历史上只挑「组代表行」送检、兄弟行借用其结论, + * 现网形状是成片空行(10-09 任务 30455 的 281 行空行 100% 属于"同父号已有一行被判")。 + */ + @Test + @SuppressWarnings("unchecked") + void everySubRowOfTheSameGroupIsClaimedIndividually() { + String threeSubRows = "[{\"rowToken\":\"r21\",\"id\":\"2_1\",\"asin\":\"B0A0000001\",\"country\":\"US\"," + + "\"title\":\"SHOE A\",\"url\":\"http://img/a.jpg\",\"groupKey\":\"g1\"}," + + "{\"rowToken\":\"r22\",\"id\":\"2_2\",\"asin\":\"B0A0000002\",\"country\":\"US\"," + + "\"title\":\"SHOE B\",\"url\":\"http://img/b.jpg\",\"groupKey\":\"g1\"}," + + "{\"rowToken\":\"r23\",\"id\":\"2_3\",\"asin\":\"B0A0000003\",\"country\":\"US\"," + + "\"title\":\"SHOE C\",\"url\":\"http://img/c.jpg\",\"groupKey\":\"g1\"}]"; + when(transientPayloadStorageService.resolvePayload(eq(POINTER), anyString())).thenReturn(threeSubRows); + when(taskCacheService.beginRowInspection(eq(TASK_ID), anyString())).thenReturn(true); + + List candidates = (List) ReflectionTestUtils + .invokeMethod(service, "collectPendingLlmCandidates", List.of(chunk()), + new AppearancePatentTaskService.ChunkRowKeyIndex()); + + assertEquals(3, candidates.size(), "同父号三行必须都进候选,不能只检一行"); + verify(taskCacheService).beginRowInspection(TASK_ID, "r21"); + verify(taskCacheService).beginRowInspection(TASK_ID, "r22"); + verify(taskCacheService).beginRowInspection(TASK_ID, "r23"); + } + + /** 兄弟行已经判完,不能反过来把没判的行"代表掉"——那正是成片空行的来源。 */ + @Test + @SuppressWarnings("unchecked") + void judgedRowDoesNotSuppressItsUnjudgedSiblings() { + String mixed = "[{\"rowToken\":\"r21\",\"id\":\"2_1\",\"asin\":\"B0A0000001\",\"country\":\"US\"," + + "\"title\":\"SHOE A\",\"url\":\"http://img/a.jpg\",\"groupKey\":\"g1\"," + + "\"titleRisk\":\"无侵权\",\"appearanceRisk\":\"无侵权\",\"conclusion\":\"无侵权\"}," + + "{\"rowToken\":\"r22\",\"id\":\"2_2\",\"asin\":\"B0A0000002\",\"country\":\"US\"," + + "\"title\":\"SHOE B\",\"url\":\"http://img/b.jpg\",\"groupKey\":\"g1\"}]"; + when(transientPayloadStorageService.resolvePayload(eq(POINTER), anyString())).thenReturn(mixed); + when(taskCacheService.beginRowInspection(eq(TASK_ID), anyString())).thenReturn(true); + + List candidates = (List) ReflectionTestUtils + .invokeMethod(service, "collectPendingLlmCandidates", List.of(chunk()), + new AppearancePatentTaskService.ChunkRowKeyIndex()); + + assertEquals(1, candidates.size()); + assertEquals("r22", candidates.get(0).getRowToken(), "该检的是没结论的那一行,不是已经检过的"); + } + + /** + * 落库不许把代表行的结论写进兄弟行:捕获真正写回对象存储的载荷文本, + * 断言兄弟行仍然没有结论字段。借用一旦回来,这条会红。 + */ + @Test + void mergeWritesBackOnlyTheLandedRowAndNeverStampsSiblings() { + String twoRows = "[{\"rowToken\":\"r21\",\"id\":\"2_1\",\"asin\":\"B0A0000001\",\"country\":\"US\"," + + "\"title\":\"SHOE A\",\"url\":\"http://img/a.jpg\",\"groupKey\":\"g1\"}," + + "{\"rowToken\":\"r22\",\"id\":\"2_2\",\"asin\":\"B0A0000002\",\"country\":\"US\"," + + "\"title\":\"SHOE B\",\"url\":\"http://img/b.jpg\",\"groupKey\":\"g1\"}]"; + when(transientPayloadStorageService.resolvePayload(eq(POINTER), anyString())).thenReturn(twoRows); + when(transientPayloadStorageService.storeChunkPayloadVersioned(anyString(), anyLong(), anyString(), anyInt(), anyString())) + .thenReturn("ptr-new"); + when(taskChunkMapper.update(any(), any())).thenReturn(1); + AppearancePatentResultRowDto landed = row("r21", "2_1", "B0A0000001"); + landed.setTitleRisk("无侵权"); + landed.setAppearanceRisk("无侵权"); + landed.setConclusion("无侵权"); + + ReflectionTestUtils.invokeMethod(service, "mergeLlmRowsIntoChunks", + task(), List.of(landed), List.of(landed), new AppearancePatentTaskService.ChunkRowKeyIndex()); + + ArgumentCaptor written = ArgumentCaptor.forClass(String.class); + verify(transientPayloadStorageService, org.mockito.Mockito.atLeastOnce()).storeChunkPayloadVersioned( + anyString(), eq(TASK_ID), anyString(), anyInt(), written.capture()); + boolean landedCarriesVerdict = false; + for (String payload : written.getAllValues()) { + assertTrue(payload.contains("\"rowToken\":\"r22\""), "兄弟行必须仍在载荷里(不许被删掉): " + payload); + int sibling = payload.indexOf("\"rowToken\":\"r22\""); + assertFalse(payload.substring(sibling).contains("无侵权"), "兄弟行不得继承代表行的结论: " + payload); + if (payload.contains("\"rowToken\":\"r21\"") && payload.contains("无侵权")) { + landedCarriesVerdict = true; + } + } + assertTrue(landedCarriesVerdict, "落库的那一行要带着自己的结论写回: " + written.getAllValues()); + } + + private AppearancePatentResultRowDto row(String rowToken, String id, String asin) { + AppearancePatentResultRowDto row = new AppearancePatentResultRowDto(); + row.setRowToken(rowToken); + row.setId(id); + row.setAsin(asin); + row.setCountry("US"); + return row; + } + private TaskChunkEntity chunk() { TaskChunkEntity chunk = new TaskChunkEntity(); chunk.setId(11L); diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentTaskCacheServiceRowMarkTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentTaskCacheServiceRowMarkTest.java index d0019738b..d3cee947a 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentTaskCacheServiceRowMarkTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentTaskCacheServiceRowMarkTest.java @@ -153,6 +153,33 @@ class AppearancePatentTaskCacheServiceRowMarkTest { verify(valueOperations, never()).set(anyString(), anyString(), any(Duration.class)); } + /** + * 交付缺口补检那条出口允许连完成标记一起撤(2026-10-10):结果落过库却又读不回来的行, + * 不撤就永远申领不到,表格里永久写着"未完成检测"。代价是那行可能被重发一次 LLM—— + * 由调用方显式传 true 承担,默认那条路(releaseRowInspection 两参版)一个字不许变。 + */ + @Test + void releaseWithCompletionClearDropsBothMarks() { + cache.releaseRowInspection(TASK_ID, List.of(ROW_KEY), true); + + ArgumentCaptor deleted = ArgumentCaptor.forClass(String.class); + verify(redisTemplate, org.mockito.Mockito.times(2)).delete(deleted.capture()); + assertTrue(deleted.getAllValues().stream().anyMatch(k -> k.contains("inflight-rows")), + "处理中键必须撤掉,否则补检轮仍然申领不到"); + assertTrue(deleted.getAllValues().stream().anyMatch(k -> k.contains("processed-rows")), + "补检出口必须把完成标记也撤掉"); + } + + /** 三参版传 false 时必须与两参版同形:已经落库结论的行不许被这条路重发。 */ + @Test + void releaseWithoutCompletionClearKeepsProcessedMark() { + cache.releaseRowInspection(TASK_ID, List.of(ROW_KEY), false); + + ArgumentCaptor deleted = ArgumentCaptor.forClass(String.class); + verify(redisTemplate).delete(deleted.capture()); + assertFalse(deleted.getValue().contains("processed-rows"), "false 分支不得删完成标记"); + } + /** 每发起一轮就自增一次:这是重发上限唯一的数据来源。 */ @Test void roundCountIncrementsPerRound() { diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentTaskServiceDelegationTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentTaskServiceDelegationTest.java index 55afff423..99c2640a7 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentTaskServiceDelegationTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/AppearancePatentTaskServiceDelegationTest.java @@ -11,6 +11,7 @@ import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResu import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentSubmitResultRequest; import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentParsedRowVo; import com.nanri.aiimage.modules.appearancepatent.service.AppearancePatentTaskCacheService; +import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentRowOutcome; import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentSheetBuilder; import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentRowNormalizer; import com.nanri.aiimage.modules.file.service.LocalFileStorageService; @@ -209,7 +210,8 @@ class AppearancePatentTaskServiceDelegationTest { try (XSSFWorkbook wb = new XSSFWorkbook(out)) { Row data = wb.getSheet("外观专利检测结果").getRow(1); - assertEquals("未取得检测结果", data.getCell(9).getStringCellValue(), "无结果行判定列显式标注(空白与没风险无法区分)"); + assertEquals(AppearancePatentRowOutcome.NO_DATA_TEXT, data.getCell(9).getStringCellValue(), + "无结果行且解析行连标题/图片都没有 ⇒ 标成「未采集到数据」,不能读成「没跑到检测」"); assertEquals("失败", data.getCell(12).getStringCellValue(), "无结果行状态判失败"); assertEquals("brand-A", data.getCell(4).getStringCellValue(), "品牌仍从解析行 values 回退"); assertEquals("", data.getCell(5).getStringCellValue(), "无结果行价格留空(resolvePrice 仅读结果行,与现状一致)"); diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentSheetBuilderTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentSheetBuilderTest.java index cb8fa407f..e97c0274d 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentSheetBuilderTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentSheetBuilderTest.java @@ -151,14 +151,14 @@ class AppearancePatentSheetBuilderTest { assertEquals("title-a", data.getCell(6).getStringCellValue(), "无结果行回退解析行标题"); assertEquals("http://img/a.jpg", data.getCell(7).getStringCellValue(), "回退解析行 URL"); assertEquals("SKU-1", data.getCell(8).getStringCellValue(), "回退解析行 sku"); - assertEquals("未取得检测结果", data.getCell(9).getStringCellValue(), "无结果行标题维度显式标注"); - assertEquals("未取得检测结果", data.getCell(10).getStringCellValue(), "无结果行外观维度显式标注"); - assertEquals("未取得检测结果", data.getCell(11).getStringCellValue(), "无结果行结论显式标注"); + assertEquals(AppearancePatentRowOutcome.NOT_INSPECTED_TEXT, data.getCell(9).getStringCellValue(), "无结果行标题维度显式标注"); + assertEquals(AppearancePatentRowOutcome.NOT_INSPECTED_TEXT, data.getCell(10).getStringCellValue(), "无结果行外观维度显式标注"); + assertEquals(AppearancePatentRowOutcome.NOT_INSPECTED_TEXT, data.getCell(11).getStringCellValue(), "无结果行结论显式标注"); assertEquals("失败", data.getCell(12).getStringCellValue(), "无结果行状态判失败,不能留空"); Row reason = wb.getSheet("原因").getRow(1); assertEquals("B01A", reason.getCell(0).getStringCellValue()); - assertTrue(reason.getCell(1).getStringCellValue().contains("未取得检测结果"), + assertTrue(reason.getCell(1).getStringCellValue().contains("未完成检测"), "原因 sheet 也要说明这行为什么没有原因: " + reason.getCell(1).getStringCellValue()); } } @@ -196,7 +196,7 @@ class AppearancePatentSheetBuilderTest { AppearancePatentSheetBuilder.rowKey(r), r)); Row data = wb.getSheet("外观专利检测结果").getRow(1); - assertEquals("未取得检测结果", data.getCell(11).getStringCellValue(), "空白结论改写成显式标注"); + assertEquals(AppearancePatentRowOutcome.NOT_INSPECTED_TEXT, data.getCell(11).getStringCellValue(), "空白结论改写成显式标注"); assertEquals("失败", data.getCell(12).getStringCellValue()); } }