From 4bcce9d5346a1476cf87f0101cb972fb8819be73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=87=AA=E8=BE=BE?= <980324341@qq.com> Date: Tue, 20 Oct 2026 14:03:18 +0800 Subject: [PATCH] =?UTF-8?q?refactor(phase-3):=203A=20=E6=8B=86=E8=96=84?= =?UTF-8?q?=E7=AC=AC=E4=BA=94=E7=AC=94=E2=80=94=E2=80=94=E5=A4=96=E8=A7=82?= =?UTF-8?q?=E4=B8=93=E5=88=A9=20LLM=20=E6=89=B9=E7=AE=A1=E7=BA=BF=E5=87=BA?= =?UTF-8?q?=E4=BB=B6=EF=BC=8C=E6=9C=8D=E5=8A=A1=203135=E2=86=922801?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit B 聚类(候选收集、批调度、轮次判死、结果行合并、批计数)原样搬进 service/support/AppearancePatentLlmBatchPipeline;读分片、分片合并、 心跳、封口判定仍留在服务侧,以回调交入。四枚同名入口保留为测试 按名反射的接缝;SourceScanTest 面数 31→32、support 8→9 并逐枚点名新件。 --- .../service/AppearancePatentTaskService.java | 378 +------------ .../AppearancePatentLlmBatchPipeline.java | 498 ++++++++++++++++++ .../AppearancePatentRowMarkTimingTest.java | 13 +- .../aiimage/testutil/SourceScanTest.java | 19 +- 4 files changed, 537 insertions(+), 371 deletions(-) create mode 100644 backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentLlmBatchPipeline.java 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 e62172ee8..3a62681d9 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 @@ -24,6 +24,7 @@ import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentSubm import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentDashboardVo; import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentHistoryItemVo; import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentHistoryAssembler; +import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentLlmBatchPipeline; import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentHistoryVo; import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentParsedGroupVo; import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentParsedRowVo; @@ -88,7 +89,6 @@ import com.nanri.aiimage.modules.task.model.vo.TaskProgressLightBatchVo; import com.nanri.aiimage.modules.task.service.TaskProgressLightAssembler; import com.nanri.aiimage.modules.task.service.pipeline.AbstractFileTaskPipeline; import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService; -import com.nanri.aiimage.modules.usersecret.support.SecretUsageContext; import com.nanri.aiimage.modules.usersecret.support.UserSecretModule; @Service @@ -783,174 +783,32 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline chunks) { - if (chunks == null || chunks.isEmpty()) { - return; - } - String prompt = readAiPrompt(task); - String apiKey = readApiKey(task); - int batchSize = Math.max(1, properties.getLlmBatchSize()); - ChunkRowKeyIndex rowKeyIndex = new ChunkRowKeyIndex(); - List candidates = terminalizeRoundExhaustedRows( - task, collectPendingLlmCandidates(chunks, rowKeyIndex), rowKeyIndex); - boolean flushRemainder = isResultSubmissionComplete(task.getId()); - if (!flushRemainder && !candidates.isEmpty()) { - LocalDateTime jobUpdatedAt = job.getUpdatedAt(); - long pendingFlushMillis = flushPendingMillis(); - if (jobUpdatedAt != null - && Duration.between(jobUpdatedAt, LocalDateTime.now()).toMillis() >= pendingFlushMillis) { - flushRemainder = true; - log.warn("[appearance-patent] 零头批次等待超时,强制提交 taskId={} jobId={} pendingRows={} batchSize={} jobUpdatedAt={} flushAfterMillis={}", - task.getId(), job.getId(), candidates.size(), batchSize, jobUpdatedAt, pendingFlushMillis); - } - } - int submitLimit = (candidates.size() / batchSize) * batchSize; - if (flushRemainder && submitLimit < candidates.size()) { - submitLimit = candidates.size(); - } - if (submitLimit <= 0) { - log.info("[appearance-patent] llm batch waiting for more rows taskId={} jobId={} pendingRows={} batchSize={} finalUpload={}", - task.getId(), job.getId(), candidates.size(), batchSize, flushRemainder); - return; - } - if (TransactionSynchronizationManager.isActualTransactionActive()) { - // 守住"整批 LLM 跑在事务里"这一类回归:下面循环每批开头的 touchRunning 是 - // UPDATE biz_task_file_job ... WHERE id=? AND status='RUNNING',本方法一旦处在事务中, - // 这把行锁就要持到全部批次结束才释放(线上实测一批 18 分钟),另一台的卡死自愈扫描 - // 于是每 110 秒撞一次 50 秒锁等待、并且整轮作废。历史上真发生过,见 - // docs/ops/prod-decisions-2026-09-28.md §9。 - log.error("[appearance-patent] llm 批次正跑在事务里,行锁会持到批次结束 taskId={} jobId={} pendingRows={}", - task.getId(), job.getId(), submitLimit); - } - int batchTotal = Math.max(1, (submitLimit + batchSize - 1) / batchSize); - int batchIndex = 1; - for (int i = 0; i < submitLimit; i += batchSize) { - List batchRows = candidates.subList(i, Math.min(i + batchSize, submitLimit)); - taskFileJobService.touchRunning(job.getId()); - List batchRowKeys = batchRows.stream().map(this::rowKey).filter(key -> !key.isBlank()).toList(); - taskCacheService.touchRowInspection(task.getId(), batchRowKeys); - taskCacheService.countRowInspectionRound(task.getId(), batchRowKeys); - long startedAt = System.currentTimeMillis(); - List llmRows; - boolean batchAnswered = true; - try { - llmRows = SecretUsageContext.call(task.getUserId(), UserSecretModule.APPEARANCE_PATENT.key(), - () -> llmClient.inspectRows(batchRows, prompt, apiKey)); - } catch (Exception ex) { - String message = firstNonBlank(ex.getMessage(), "LLM 检测失败"); - log.warn("[appearance-patent] llm batch failed taskId={} jobId={} rows={} batch={}/{} err={}", - task.getId(), job.getId(), batchRows.size(), batchIndex, batchTotal, message); - llmRows = llmClient.markRowsFailed(batchRows, message); - batchAnswered = false; - } - List keysToRetry = new ArrayList<>(batchRowKeys); - try { - Set landedKeys = mergeLlmRowsIntoChunks(task, llmRows, batchRows, rowKeyIndex); - // 完成标记只能打在真正落库的行上——这是「发版重启会不会丢行」的分界线 - if (batchAnswered) { - List landed = batchRowKeys.stream().filter(landedKeys::contains).toList(); - if (!landed.isEmpty()) { - taskCacheService.completeRowInspection(task.getId(), landed); - } - keysToRetry.removeAll(landed); - } - } finally { - // 本批结束时处理中名额必须让出来:没落库的行撤销后下一轮马上能重新申领,不等 TTL - if (!keysToRetry.isEmpty()) { - taskCacheService.releaseRowInspection(task.getId(), keysToRetry); - if (batchAnswered) { - // 结果拿到了却落不进分片(少回、rowToken 被改写、分片已清): - // 打成已完成就是"扣了次数永久没结论",宁可重发一轮。 - log.warn("[appearance-patent] 本批结果未全部落库,撤销已检测标记交由下一轮重发 taskId={} jobId={} batch={}/{} sent={} retrying={}", - task.getId(), job.getId(), batchIndex, batchTotal, batchRowKeys.size(), keysToRetry); - } - } - } - log.info("[appearance-patent] llm batch done taskId={} jobId={} rows={} batch={}/{} costMs={}", - task.getId(), job.getId(), batchRows.size(), batchIndex, batchTotal, - System.currentTimeMillis() - startedAt); - // 每批刷新任务心跳:软恢复后的检测阶段可达数小时,不刷会被通用兜底线按 updated_at 超 120 分钟误杀(2026-09-28 29473/29479) - touchJavaSideTaskActivity(task.getId()); - batchIndex++; - } + llmBatchPipeline().submitLlmBatches(task, result, job, chunks); } - /** - * 轮次已到顶的行就地判失败并打成「已完成」,从此退出候选。 - * - *

失败行会被撤销处理中标记、下一轮重新申领——这是「不丢行」的另一半代价。没有这道闸的话, - * 上游劣化窗口内同一批行会一轮轮重发,每轮都过 {@link SecretUsageContext} 扣用户的 LLM 次数, - * 而 assemble job 走的是 deferRunning(不累加 retryCount),MAX_RETRY_COUNT 那条永不触发。 - */ - private List terminalizeRoundExhaustedRows( - FileTaskEntity task, - List candidates, - ChunkRowKeyIndex rowKeyIndex) { - int maxRounds = properties.getLlmRowMaxRounds(); - if (maxRounds <= 0 || candidates == null || candidates.isEmpty()) { - return candidates == null ? List.of() : candidates; - } - List retryable = new ArrayList<>(candidates.size()); - List exhausted = new ArrayList<>(); - for (AppearancePatentResultRowDto row : candidates) { - String key = rowKey(row); - if (!key.isBlank() && taskCacheService.rowInspectionRounds(task.getId(), key) >= maxRounds) { - exhausted.add(row); - } else { - retryable.add(row); - } - } - if (exhausted.isEmpty()) { - return retryable; - } - String reason = "重发轮次已达上限 " + maxRounds + " 轮仍未取到结论,请稍后重新发起检测"; - log.warn("[appearance-patent] llm round cap reached taskId={} rows={} maxRounds={}", - task.getId(), exhausted.size(), maxRounds); - List exhaustedKeys = exhausted.stream().map(this::rowKey).filter(key -> !key.isBlank()).toList(); - boolean rowsTerminated = false; - try { - mergeLlmRowsIntoChunks(task, llmClient.markRowsFailed(exhausted, reason), - exhausted, rowKeyIndex); - rowsTerminated = true; - } finally { - // 失败原因没落库就不能打成「已完成」,否则这批行既没结论也再不会重试 - if (rowsTerminated) { - taskCacheService.completeRowInspection(task.getId(), exhaustedKeys); - } else { - taskCacheService.releaseRowInspection(task.getId(), exhaustedKeys); - } - } - return retryable; + private List collectPendingLlmCandidates( + List chunks, AppearancePatentLlmBatchPipeline.ChunkRowKeyIndex rowKeyIndex) { + return llmBatchPipeline().collectPendingLlmCandidates(chunks, rowKeyIndex); + } + + private int countUninspectedRows(List chunks) { + return llmBatchPipeline().countUninspectedRows(chunks); } private Set mergeLlmRowsIntoChunks(FileTaskEntity task, List llmRows, List batchRows, - ChunkRowKeyIndex rowKeyIndex) { - if (llmRows == null || llmRows.isEmpty()) { - return Set.of(); - } - Map deduped = new LinkedHashMap<>(); - for (int i = 0; i < llmRows.size(); i++) { - AppearancePatentResultRowDto row = llmRows.get(i); - AppearancePatentResultRowDto sourceRow = i < batchRows.size() ? batchRows.get(i) : null; - String key = rowKey(row); - if (key.isBlank()) { - if (sourceRow != null) { - key = rowKey(sourceRow); - } - } - if (key.isBlank() || !deduped.containsKey(key)) { - deduped.put(key, row); - } - } - return mergeLlmRowsIntoSubmittedChunks(task, new ArrayList<>(deduped.values()), null, null, rowKeyIndex); + AppearancePatentLlmBatchPipeline.ChunkRowKeyIndex rowKeyIndex) { + return llmBatchPipeline().mergeLlmRowsIntoChunks(task, llmRows, batchRows, rowKeyIndex); } /** @@ -968,108 +826,12 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline历史上这里只挑「组代表行」,其余兄弟行指望 {@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()); - chunkRows += persistedRows.size(); - for (AppearancePatentResultRowDto row : persistedRows.values()) { - String key = rowKey(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; - } - - /** - * 只计数、不申领。用 {@link #collectPendingLlmCandidates} 计数会把行标成处理中, - * 紧接着真正发起检测的那一步就一行也申领不到,只能等处理中标记的 TTL 过期。 - */ - private int countUninspectedRows(List chunks) { - if (chunks == null || chunks.isEmpty()) { - return 0; - } - int total = 0; - for (TaskChunkEntity chunk : chunks) { - Map persistedRows = readChunkRows(chunk); - for (AppearancePatentResultRowDto row : persistedRows.values()) { - if (!rowKey(row).isBlank() && !AppearancePatentRowOutcome.isJudged(row)) { - total++; - } - } - } - return total; - } - - private int countLlmWorkUnits(List chunks, int batchSize) { - if (chunks == null || chunks.isEmpty()) { - return 0; - } - int total = 0; - for (TaskChunkEntity chunk : chunks) { - Map persistedRows = readChunkRows(chunk); - if (persistedRows.isEmpty()) { - continue; - } - 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); - } - } - return total; - } - - private long flushPendingMillis() { - return Math.max(1, properties.getFlushPendingMinutes()) * 60_000L; + private AppearancePatentLlmBatchPipeline llmBatchPipeline() { + return new AppearancePatentLlmBatchPipeline( + properties, llmClient, taskCacheService, taskFileJobService, + this::rowKey, this::shouldInspectRow, this::readChunkRows, this::loadSubmittedChunks, + this::isResultSubmissionComplete, this::touchJavaSideTaskActivity, + this::readAiPrompt, this::readApiKey, this::mergeChunkPayload, this::firstNonBlank); } private void submitLlmForSubmittedChunk(SubmitContext context) { @@ -1680,7 +1442,7 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline mergeLlmRowsIntoSubmittedChunks(FileTaskEntity task, - List llmRows, - String fallbackScopeHash, - Integer fallbackChunkIndex, - ChunkRowKeyIndex rowKeyIndex) { - if (task == null || llmRows == null || llmRows.isEmpty()) { - return Set.of(); - } - List chunks = loadSubmittedChunks(task.getId()); - if (chunks.isEmpty()) { - return Set.of(); - } - Map> rowKeysByChunk = new LinkedHashMap<>(); - Map chunkByKey = new LinkedHashMap<>(); - for (TaskChunkEntity chunk : chunks) { - String chunkKey = chunkStorageKey(chunk.getScopeHash(), chunk.getChunkIndex()); - rowKeysByChunk.put(chunkKey, chunkRowKeySet(rowKeyIndex, chunk)); - chunkByKey.put(chunkKey, chunk); - } - Map> mergeRowsByChunk = new LinkedHashMap<>(); - for (AppearancePatentResultRowDto resultRow : llmRows) { - for (AppearancePatentResultRowDto expandedRow : List.of(resultRow)) { - String rowKey = rowKey(expandedRow); - if (rowKey.isBlank()) { - continue; - } - boolean matched = false; - for (Map.Entry> entry : rowKeysByChunk.entrySet()) { - if (entry.getValue().contains(rowKey)) { - mergeRowsByChunk.computeIfAbsent(entry.getKey(), ignored -> new LinkedHashMap<>()) - .put(rowKey, expandedRow); - matched = true; - } - } - if (!matched && fallbackScopeHash != null && fallbackChunkIndex != null) { - String fallbackKey = chunkStorageKey(fallbackScopeHash, fallbackChunkIndex); - if (chunkByKey.containsKey(fallbackKey)) { - mergeRowsByChunk.computeIfAbsent(fallbackKey, ignored -> new LinkedHashMap<>()) - .put(rowKey, expandedRow); - matched = true; - } - } - if (!matched) { - log.warn("[appearance-patent] llm row has no submitted chunk taskId={} rowKey={}", - task.getId(), rowKey); - } - } - } - Set landedKeys = new LinkedHashSet<>(); - for (Map.Entry> entry : mergeRowsByChunk.entrySet()) { - TaskChunkEntity chunk = chunkByKey.get(entry.getKey()); - if (chunk == null || entry.getValue().isEmpty()) { - continue; - } - landedKeys.addAll(mergeChunkPayload(task.getId(), chunk.getScopeHash(), chunk.getChunkIndex(), - new ArrayList<>(entry.getValue().values()))); - } - return landedKeys; - } - - private String chunkStorageKey(String scopeHash, Integer chunkIndex) { - return firstNonBlank(scopeHash, "") + ":" + (chunkIndex == null ? 0 : chunkIndex); - } - - /** - * 一次 {@link #submitLlmBatches} 内共享的「哪个分片里有哪些行」索引,按 payload_hash 判新鲜。 - * - *

只存 rowKey 不存整行,索引本身不会把载荷钉在内存里。作用域必须是单次调用(局部变量,随栈丢弃): - * 本服务是单例、多个任务同时在跑批,做成字段就是多线程共享一个裸 LinkedHashMap,而且分片键永不过期、只增不减。 - * 数据正确性反过来不依赖局部性——收集候选那一遍每次都会重读并重记全部分片。 - */ - static final class ChunkRowKeyIndex { - private final Map snapshots = new LinkedHashMap<>(); - } - - /** 把刚解析出来的分片行键记进索引(自己拷一份,不握住整行载荷),返回索引里那份。 */ - private Set rememberChunkRowKeys(ChunkRowKeyIndex rowKeyIndex, TaskChunkEntity chunk, Set rowKeys) { - Set copied = new LinkedHashSet<>(rowKeys); - rowKeyIndex.snapshots.put(chunkStorageKey(chunk.getScopeHash(), chunk.getChunkIndex()), - new Snapshot(chunk.getPayloadHash(), copied)); - return copied; - } - - /** 分片当前载荷里的全部行键;载荷哈希与索引里那次解析一致时直接复用,不重读对象存储。 */ - private Set chunkRowKeySet(ChunkRowKeyIndex rowKeyIndex, TaskChunkEntity chunk) { - Snapshot cached = rowKeyIndex.snapshots.get(chunkStorageKey(chunk.getScopeHash(), chunk.getChunkIndex())); - if (cached != null && Objects.equals(cached.payloadHash, chunk.getPayloadHash())) { - return cached.rowKeys; - } - return rememberChunkRowKeys(rowKeyIndex, chunk, readChunkRows(chunk).keySet()); - } - - private record Snapshot(String payloadHash, Set rowKeys) { - } - private boolean isResultSubmissionComplete(Long taskId) { if (taskId == null || taskId <= 0) { return false; diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentLlmBatchPipeline.java b/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentLlmBatchPipeline.java new file mode 100644 index 000000000..8a133e413 --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentLlmBatchPipeline.java @@ -0,0 +1,498 @@ +package com.nanri.aiimage.modules.appearancepatent.service.support; + +import com.nanri.aiimage.config.AppearancePatentProperties; +import com.nanri.aiimage.modules.appearancepatent.client.AppearancePatentLlmClient; +import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto; +import com.nanri.aiimage.modules.appearancepatent.service.AppearancePatentTaskCacheService; +import com.nanri.aiimage.modules.task.model.entity.FileResultEntity; +import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity; +import com.nanri.aiimage.modules.task.model.entity.TaskChunkEntity; +import com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity; +import com.nanri.aiimage.modules.task.service.TaskFileJobService; +import com.nanri.aiimage.modules.usersecret.support.SecretUsageContext; +import com.nanri.aiimage.modules.usersecret.support.UserSecretModule; +import lombok.extern.slf4j.Slf4j; +import org.springframework.transaction.support.TransactionSynchronizationManager; + +import java.time.Duration; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.function.BiFunction; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Predicate; + +/** + * 外观专利的 LLM 批管线(B 聚类),从 {@code AppearancePatentTaskService} 原样搬移 + * (3A 拆薄第五笔):候选收集、批调度、轮次到顶判死、结果行合并回分片、批工作单元计数, + * 连同它们的日志文案与判序一个字没改。 + * + *

搬的是批管线,不是它的宿主能力。读分片载荷({@code readChunkRows})、分片合并 + * ({@code mergeChunkPayload})、任务心跳({@code touchJavaSideTaskActivity})、提交是否封口 + * ({@code isResultSubmissionComplete})都还留在服务侧,以回调交进来——它们分属 D/G/H 聚类, + * 跟着 B 一起拖出来就得先给它们再造一套构造器,而 3A 的账已经证明 + * 「结构性成本 > 可沉件收益」是要避免的事。回调是这一笔里唯一的新形状。 + * + *

作用域必须是单次调用。本类是单例服务里的临时协作者(每次入口新建), + * {@link ChunkRowKeyIndex} 随栈丢弃;做成服务字段就是多任务共享一个裸 LinkedHashMap, + * 而且分片键永不过期、只增不减(原注释里那条约束一并带过来)。 + * + *

搬移后唯一的可观测差异是 logger 名:从 + * {@code ...service.AppearancePatentTaskService} 变为 + * {@code ...service.support.AppearancePatentLlmBatchPipeline},消息文本、级别与参数顺序未动。 + */ +@Slf4j +public class AppearancePatentLlmBatchPipeline { + + /** 分片载荷合并(D 聚类)留在服务侧;返回真正写进这一片载荷的行键。 */ + @FunctionalInterface + public interface ChunkRowMerger { + List merge(Long taskId, String scopeHash, Integer chunkIndex, + List rows); + } + + private final AppearancePatentProperties properties; + private final AppearancePatentLlmClient llmClient; + private final AppearancePatentTaskCacheService taskCacheService; + private final TaskFileJobService taskFileJobService; + private final Function rowKeyer; + private final Predicate shouldInspect; + private final Function> chunkRowReader; + private final Function> submittedChunksLoader; + private final Predicate submissionComplete; + private final Consumer taskActivityToucher; + private final Function promptReader; + private final Function apiKeyReader; + private final ChunkRowMerger chunkRowMerger; + private final BiFunction firstNonBlankFn; + + public AppearancePatentLlmBatchPipeline(AppearancePatentProperties properties, + AppearancePatentLlmClient llmClient, + AppearancePatentTaskCacheService taskCacheService, + TaskFileJobService taskFileJobService, + Function rowKeyer, + Predicate shouldInspect, + Function> chunkRowReader, + Function> submittedChunksLoader, + Predicate submissionComplete, + Consumer taskActivityToucher, + Function promptReader, + Function apiKeyReader, + ChunkRowMerger chunkRowMerger, + BiFunction firstNonBlankFn) { + this.properties = properties; + this.llmClient = llmClient; + this.taskCacheService = taskCacheService; + this.taskFileJobService = taskFileJobService; + this.rowKeyer = rowKeyer; + this.shouldInspect = shouldInspect; + this.chunkRowReader = chunkRowReader; + this.submittedChunksLoader = submittedChunksLoader; + this.submissionComplete = submissionComplete; + this.taskActivityToucher = taskActivityToucher; + this.promptReader = promptReader; + this.apiKeyReader = apiKeyReader; + this.chunkRowMerger = chunkRowMerger; + this.firstNonBlankFn = firstNonBlankFn; + } + + /** + * 同步检测:按批次串行调用 LLM(批内行级并发由 client 负责), + * 结果直接合并回 chunk。批间去重用 Redis 行级标记防止 submitResult 与 stale 恢复双入口重复检测。 + */ + public void submitLlmBatches(FileTaskEntity task, + FileResultEntity result, + TaskFileJobEntity job, + List chunks) { + if (chunks == null || chunks.isEmpty()) { + return; + } + String prompt = promptReader.apply(task); + String apiKey = apiKeyReader.apply(task); + int batchSize = Math.max(1, properties.getLlmBatchSize()); + ChunkRowKeyIndex rowKeyIndex = new ChunkRowKeyIndex(); + List candidates = terminalizeRoundExhaustedRows( + task, collectPendingLlmCandidates(chunks, rowKeyIndex), rowKeyIndex); + boolean flushRemainder = isResultSubmissionComplete(task.getId()); + if (!flushRemainder && !candidates.isEmpty()) { + LocalDateTime jobUpdatedAt = job.getUpdatedAt(); + long pendingFlushMillis = flushPendingMillis(); + if (jobUpdatedAt != null + && Duration.between(jobUpdatedAt, LocalDateTime.now()).toMillis() >= pendingFlushMillis) { + flushRemainder = true; + log.warn("[appearance-patent] 零头批次等待超时,强制提交 taskId={} jobId={} pendingRows={} batchSize={} jobUpdatedAt={} flushAfterMillis={}", + task.getId(), job.getId(), candidates.size(), batchSize, jobUpdatedAt, pendingFlushMillis); + } + } + int submitLimit = (candidates.size() / batchSize) * batchSize; + if (flushRemainder && submitLimit < candidates.size()) { + submitLimit = candidates.size(); + } + if (submitLimit <= 0) { + log.info("[appearance-patent] llm batch waiting for more rows taskId={} jobId={} pendingRows={} batchSize={} finalUpload={}", + task.getId(), job.getId(), candidates.size(), batchSize, flushRemainder); + return; + } + if (TransactionSynchronizationManager.isActualTransactionActive()) { + // 守住"整批 LLM 跑在事务里"这一类回归:下面循环每批开头的 touchRunning 是 + // UPDATE biz_task_file_job ... WHERE id=? AND status='RUNNING',本方法一旦处在事务中, + // 这把行锁就要持到全部批次结束才释放(线上实测一批 18 分钟),另一台的卡死自愈扫描 + // 于是每 110 秒撞一次 50 秒锁等待、并且整轮作废。历史上真发生过,见 + // docs/ops/prod-decisions-2026-09-28.md §9。 + log.error("[appearance-patent] llm 批次正跑在事务里,行锁会持到批次结束 taskId={} jobId={} pendingRows={}", + task.getId(), job.getId(), submitLimit); + } + int batchTotal = Math.max(1, (submitLimit + batchSize - 1) / batchSize); + int batchIndex = 1; + for (int i = 0; i < submitLimit; i += batchSize) { + List batchRows = candidates.subList(i, Math.min(i + batchSize, submitLimit)); + taskFileJobService.touchRunning(job.getId()); + List batchRowKeys = batchRows.stream().map(this::rowKey).filter(key -> !key.isBlank()).toList(); + taskCacheService.touchRowInspection(task.getId(), batchRowKeys); + taskCacheService.countRowInspectionRound(task.getId(), batchRowKeys); + long startedAt = System.currentTimeMillis(); + List llmRows; + boolean batchAnswered = true; + try { + llmRows = SecretUsageContext.call(task.getUserId(), UserSecretModule.APPEARANCE_PATENT.key(), + () -> llmClient.inspectRows(batchRows, prompt, apiKey)); + } catch (Exception ex) { + String message = firstNonBlank(ex.getMessage(), "LLM 检测失败"); + log.warn("[appearance-patent] llm batch failed taskId={} jobId={} rows={} batch={}/{} err={}", + task.getId(), job.getId(), batchRows.size(), batchIndex, batchTotal, message); + llmRows = llmClient.markRowsFailed(batchRows, message); + batchAnswered = false; + } + List keysToRetry = new ArrayList<>(batchRowKeys); + try { + Set landedKeys = mergeLlmRowsIntoChunks(task, llmRows, batchRows, rowKeyIndex); + // 完成标记只能打在真正落库的行上——这是「发版重启会不会丢行」的分界线 + if (batchAnswered) { + List landed = batchRowKeys.stream().filter(landedKeys::contains).toList(); + if (!landed.isEmpty()) { + taskCacheService.completeRowInspection(task.getId(), landed); + } + keysToRetry.removeAll(landed); + } + } finally { + // 本批结束时处理中名额必须让出来:没落库的行撤销后下一轮马上能重新申领,不等 TTL + if (!keysToRetry.isEmpty()) { + taskCacheService.releaseRowInspection(task.getId(), keysToRetry); + if (batchAnswered) { + // 结果拿到了却落不进分片(少回、rowToken 被改写、分片已清): + // 打成已完成就是"扣了次数永久没结论",宁可重发一轮。 + log.warn("[appearance-patent] 本批结果未全部落库,撤销已检测标记交由下一轮重发 taskId={} jobId={} batch={}/{} sent={} retrying={}", + task.getId(), job.getId(), batchIndex, batchTotal, batchRowKeys.size(), keysToRetry); + } + } + } + log.info("[appearance-patent] llm batch done taskId={} jobId={} rows={} batch={}/{} costMs={}", + task.getId(), job.getId(), batchRows.size(), batchIndex, batchTotal, + System.currentTimeMillis() - startedAt); + // 每批刷新任务心跳:软恢复后的检测阶段可达数小时,不刷会被通用兜底线按 updated_at 超 120 分钟误杀(2026-09-28 29473/29479) + taskActivityToucher.accept(task.getId()); + batchIndex++; + } + } + + /** + * 轮次已到顶的行就地判失败并打成「已完成」,从此退出候选。 + * + *

失败行会被撤销处理中标记、下一轮重新申领——这是「不丢行」的另一半代价。没有这道闸的话, + * 上游劣化窗口内同一批行会一轮轮重发,每轮都过 {@link SecretUsageContext} 扣用户的 LLM 次数, + * 而 assemble job 走的是 deferRunning(不累加 retryCount),MAX_RETRY_COUNT 那条永不触发。 + */ + private List terminalizeRoundExhaustedRows( + FileTaskEntity task, + List candidates, + ChunkRowKeyIndex rowKeyIndex) { + int maxRounds = properties.getLlmRowMaxRounds(); + if (maxRounds <= 0 || candidates == null || candidates.isEmpty()) { + return candidates == null ? List.of() : candidates; + } + List retryable = new ArrayList<>(candidates.size()); + List exhausted = new ArrayList<>(); + for (AppearancePatentResultRowDto row : candidates) { + String key = rowKey(row); + if (!key.isBlank() && taskCacheService.rowInspectionRounds(task.getId(), key) >= maxRounds) { + exhausted.add(row); + } else { + retryable.add(row); + } + } + if (exhausted.isEmpty()) { + return retryable; + } + String reason = "重发轮次已达上限 " + maxRounds + " 轮仍未取到结论,请稍后重新发起检测"; + log.warn("[appearance-patent] llm round cap reached taskId={} rows={} maxRounds={}", + task.getId(), exhausted.size(), maxRounds); + List exhaustedKeys = exhausted.stream().map(this::rowKey).filter(key -> !key.isBlank()).toList(); + boolean rowsTerminated = false; + try { + mergeLlmRowsIntoChunks(task, llmClient.markRowsFailed(exhausted, reason), + exhausted, rowKeyIndex); + rowsTerminated = true; + } finally { + // 失败原因没落库就不能打成「已完成」,否则这批行既没结论也再不会重试 + if (rowsTerminated) { + taskCacheService.completeRowInspection(task.getId(), exhaustedKeys); + } else { + taskCacheService.releaseRowInspection(task.getId(), exhaustedKeys); + } + } + return retryable; + } + + public Set mergeLlmRowsIntoChunks(FileTaskEntity task, + List llmRows, + List batchRows, + ChunkRowKeyIndex rowKeyIndex) { + if (llmRows == null || llmRows.isEmpty()) { + return Set.of(); + } + Map deduped = new LinkedHashMap<>(); + for (int i = 0; i < llmRows.size(); i++) { + AppearancePatentResultRowDto row = llmRows.get(i); + AppearancePatentResultRowDto sourceRow = i < batchRows.size() ? batchRows.get(i) : null; + String key = rowKey(row); + if (key.isBlank()) { + if (sourceRow != null) { + key = rowKey(sourceRow); + } + } + if (key.isBlank() || !deduped.containsKey(key)) { + deduped.put(key, row); + } + } + return mergeLlmRowsIntoSubmittedChunks(task, new ArrayList<>(deduped.values()), null, null, rowKeyIndex); + } + + /** + * 收集未检测的候选行:**逐行**申领——同一父号下的 {@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 的行会被判"已检测",从此退出候选,而表格里它那三列全是空的。 + */ + public 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()); + chunkRows += persistedRows.size(); + for (AppearancePatentResultRowDto row : persistedRows.values()) { + String key = rowKey(row); + if (key.isBlank()) { + continue; + } + if (AppearancePatentRowOutcome.isJudged(row)) { + judgedSkipped++; + continue; + } + if (!shouldInspect.test(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; + } + + /** + * 只计数、不申领。用 {@link #collectPendingLlmCandidates} 计数会把行标成处理中, + * 紧接着真正发起检测的那一步就一行也申领不到,只能等处理中标记的 TTL 过期。 + */ + public int countUninspectedRows(List chunks) { + if (chunks == null || chunks.isEmpty()) { + return 0; + } + int total = 0; + for (TaskChunkEntity chunk : chunks) { + Map persistedRows = readChunkRows(chunk); + for (AppearancePatentResultRowDto row : persistedRows.values()) { + if (!rowKey(row).isBlank() && !AppearancePatentRowOutcome.isJudged(row)) { + total++; + } + } + } + return total; + } + + public int countLlmWorkUnits(List chunks, int batchSize) { + if (chunks == null || chunks.isEmpty()) { + return 0; + } + int total = 0; + for (TaskChunkEntity chunk : chunks) { + Map persistedRows = readChunkRows(chunk); + if (persistedRows.isEmpty()) { + continue; + } + 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); + } + } + return total; + } + + private long flushPendingMillis() { + return Math.max(1, properties.getFlushPendingMinutes()) * 60_000L; + } + + /** 返回真正落库的行键;没匹配到分片行的键不会出现在里面,调用方据此决定要不要撤销已检测标记。 */ + private Set mergeLlmRowsIntoSubmittedChunks(FileTaskEntity task, + List llmRows, + String fallbackScopeHash, + Integer fallbackChunkIndex, + ChunkRowKeyIndex rowKeyIndex) { + if (task == null || llmRows == null || llmRows.isEmpty()) { + return Set.of(); + } + List chunks = loadSubmittedChunks(task.getId()); + if (chunks.isEmpty()) { + return Set.of(); + } + Map> rowKeysByChunk = new LinkedHashMap<>(); + Map chunkByKey = new LinkedHashMap<>(); + for (TaskChunkEntity chunk : chunks) { + String chunkKey = chunkStorageKey(chunk.getScopeHash(), chunk.getChunkIndex()); + rowKeysByChunk.put(chunkKey, chunkRowKeySet(rowKeyIndex, chunk)); + chunkByKey.put(chunkKey, chunk); + } + Map> mergeRowsByChunk = new LinkedHashMap<>(); + for (AppearancePatentResultRowDto resultRow : llmRows) { + for (AppearancePatentResultRowDto expandedRow : List.of(resultRow)) { + String rowKey = rowKey(expandedRow); + if (rowKey.isBlank()) { + continue; + } + boolean matched = false; + for (Map.Entry> entry : rowKeysByChunk.entrySet()) { + if (entry.getValue().contains(rowKey)) { + mergeRowsByChunk.computeIfAbsent(entry.getKey(), ignored -> new LinkedHashMap<>()) + .put(rowKey, expandedRow); + matched = true; + } + } + if (!matched && fallbackScopeHash != null && fallbackChunkIndex != null) { + String fallbackKey = chunkStorageKey(fallbackScopeHash, fallbackChunkIndex); + if (chunkByKey.containsKey(fallbackKey)) { + mergeRowsByChunk.computeIfAbsent(fallbackKey, ignored -> new LinkedHashMap<>()) + .put(rowKey, expandedRow); + matched = true; + } + } + if (!matched) { + log.warn("[appearance-patent] llm row has no submitted chunk taskId={} rowKey={}", + task.getId(), rowKey); + } + } + } + Set landedKeys = new LinkedHashSet<>(); + for (Map.Entry> entry : mergeRowsByChunk.entrySet()) { + TaskChunkEntity chunk = chunkByKey.get(entry.getKey()); + if (chunk == null || entry.getValue().isEmpty()) { + continue; + } + landedKeys.addAll(chunkRowMerger.merge(task.getId(), chunk.getScopeHash(), chunk.getChunkIndex(), + new ArrayList<>(entry.getValue().values()))); + } + return landedKeys; + } + + private String chunkStorageKey(String scopeHash, Integer chunkIndex) { + return firstNonBlank(scopeHash, "") + ":" + (chunkIndex == null ? 0 : chunkIndex); + } + + /** + * 一次 {@link #submitLlmBatches} 内共享的「哪个分片里有哪些行」索引,按 payload_hash 判新鲜。 + * + *

只存 rowKey 不存整行,索引本身不会把载荷钉在内存里。作用域必须是单次调用(局部变量,随栈丢弃): + * 本服务是单例、多个任务同时在跑批,做成字段就是多线程共享一个裸 LinkedHashMap,而且分片键永不过期、只增不减。 + * 数据正确性反过来不依赖局部性——收集候选那一遍每次都会重读并重记全部分片。 + */ + public static final class ChunkRowKeyIndex { + private final Map snapshots = new LinkedHashMap<>(); + } + + /** 把刚解析出来的分片行键记进索引(自己拷一份,不握住整行载荷),返回索引里那份。 */ + private Set rememberChunkRowKeys(ChunkRowKeyIndex rowKeyIndex, TaskChunkEntity chunk, Set rowKeys) { + Set copied = new LinkedHashSet<>(rowKeys); + rowKeyIndex.snapshots.put(chunkStorageKey(chunk.getScopeHash(), chunk.getChunkIndex()), + new Snapshot(chunk.getPayloadHash(), copied)); + return copied; + } + + /** 分片当前载荷里的全部行键;载荷哈希与索引里那次解析一致时直接复用,不重读对象存储。 */ + private Set chunkRowKeySet(ChunkRowKeyIndex rowKeyIndex, TaskChunkEntity chunk) { + Snapshot cached = rowKeyIndex.snapshots.get(chunkStorageKey(chunk.getScopeHash(), chunk.getChunkIndex())); + if (cached != null && Objects.equals(cached.payloadHash, chunk.getPayloadHash())) { + return cached.rowKeys; + } + return rememberChunkRowKeys(rowKeyIndex, chunk, readChunkRows(chunk).keySet()); + } + + private record Snapshot(String payloadHash, Set rowKeys) { + } + + private boolean isResultSubmissionComplete(Long taskId) { + return submissionComplete.test(taskId); + } + + private void touchJavaSideTaskActivity(Long taskId) { + taskActivityToucher.accept(taskId); + } + + private String rowKey(AppearancePatentResultRowDto row) { + return rowKeyer.apply(row); + } + + private Map readChunkRows(TaskChunkEntity chunk) { + return chunkRowReader.apply(chunk); + } + + private List loadSubmittedChunks(Long taskId) { + return submittedChunksLoader.apply(taskId); + } + + private String firstNonBlank(String preferred, String fallback) { + return firstNonBlankFn.apply(preferred, fallback); + } +} 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 8c171974d..7c7e2438c 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 @@ -10,6 +10,7 @@ import com.nanri.aiimage.config.InstanceMetadata; import com.nanri.aiimage.config.StorageProperties; import com.nanri.aiimage.modules.appearancepatent.client.AppearancePatentLlmClient; import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto; +import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentLlmBatchPipeline; import com.nanri.aiimage.modules.file.service.LocalFileStorageService; import com.nanri.aiimage.modules.file.service.oss.OssStorageService; import com.nanri.aiimage.modules.task.mapper.FileResultMapper; @@ -137,7 +138,7 @@ class AppearancePatentRowMarkTimingTest { when(taskCacheService.beginRowInspection(eq(TASK_ID), anyString())).thenReturn(true); List candidates = (List) ReflectionTestUtils - .invokeMethod(service, "collectPendingLlmCandidates", List.of(chunk()), new AppearancePatentTaskService.ChunkRowKeyIndex()); + .invokeMethod(service, "collectPendingLlmCandidates", List.of(chunk()), new AppearancePatentLlmBatchPipeline.ChunkRowKeyIndex()); assertEquals(2, candidates.size()); verify(taskCacheService).beginRowInspection(TASK_ID, "r1"); @@ -153,7 +154,7 @@ class AppearancePatentRowMarkTimingTest { when(taskCacheService.beginRowInspection(eq(TASK_ID), eq("r2"))).thenReturn(false); List candidates = (List) ReflectionTestUtils - .invokeMethod(service, "collectPendingLlmCandidates", List.of(chunk()), new AppearancePatentTaskService.ChunkRowKeyIndex()); + .invokeMethod(service, "collectPendingLlmCandidates", List.of(chunk()), new AppearancePatentLlmBatchPipeline.ChunkRowKeyIndex()); assertEquals(1, candidates.size()); assertEquals("r1", candidates.get(0).getRowToken()); @@ -362,7 +363,7 @@ class AppearancePatentRowMarkTimingTest { when(taskCacheService.beginRowInspection(eq(TASK_ID), anyString())).thenReturn(true); List candidates = (List) ReflectionTestUtils - .invokeMethod(service, "collectPendingLlmCandidates", List.of(chunk()), new AppearancePatentTaskService.ChunkRowKeyIndex()); + .invokeMethod(service, "collectPendingLlmCandidates", List.of(chunk()), new AppearancePatentLlmBatchPipeline.ChunkRowKeyIndex()); assertEquals(1, candidates.size()); assertEquals("r3", candidates.get(0).getRowToken()); @@ -387,7 +388,7 @@ class AppearancePatentRowMarkTimingTest { List candidates = (List) ReflectionTestUtils .invokeMethod(service, "collectPendingLlmCandidates", List.of(chunk()), - new AppearancePatentTaskService.ChunkRowKeyIndex()); + new AppearancePatentLlmBatchPipeline.ChunkRowKeyIndex()); assertEquals(3, candidates.size(), "同父号三行必须都进候选,不能只检一行"); verify(taskCacheService).beginRowInspection(TASK_ID, "r21"); @@ -409,7 +410,7 @@ class AppearancePatentRowMarkTimingTest { List candidates = (List) ReflectionTestUtils .invokeMethod(service, "collectPendingLlmCandidates", List.of(chunk()), - new AppearancePatentTaskService.ChunkRowKeyIndex()); + new AppearancePatentLlmBatchPipeline.ChunkRowKeyIndex()); assertEquals(1, candidates.size()); assertEquals("r22", candidates.get(0).getRowToken(), "该检的是没结论的那一行,不是已经检过的"); @@ -435,7 +436,7 @@ class AppearancePatentRowMarkTimingTest { landed.setConclusion("无侵权"); ReflectionTestUtils.invokeMethod(service, "mergeLlmRowsIntoChunks", - task(), List.of(landed), List.of(landed), new AppearancePatentTaskService.ChunkRowKeyIndex()); + task(), List.of(landed), List.of(landed), new AppearancePatentLlmBatchPipeline.ChunkRowKeyIndex()); ArgumentCaptor written = ArgumentCaptor.forClass(String.class); verify(transientPayloadStorageService, org.mockito.Mockito.atLeastOnce()).storeChunkPayloadVersioned( diff --git a/backend-java/src/test/java/com/nanri/aiimage/testutil/SourceScanTest.java b/backend-java/src/test/java/com/nanri/aiimage/testutil/SourceScanTest.java index 5209401a7..1c8d08f69 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/testutil/SourceScanTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/testutil/SourceScanTest.java @@ -266,13 +266,13 @@ class SourceScanTest { // ---------------------------------------------------------------- N6 moduleSources @Test - void moduleSources_appearancepatent返回31支含拆分后的support件() { + void moduleSources_appearancepatent返回32支含拆分后的support件() { List files = SourceScan.moduleSources("modules/appearancepatent"); assertThat(files) - .as("真实集成:对真仓目录求值。31 支=1 controller+1 client+1 service 主件+3 spi/cache+" - + "8 service/support+16 model(dto7/vo9),与 find 实测同号") - .hasSize(31); + .as("真实集成:对真仓目录求值。32 支=1 controller+1 client+1 service 主件+3 spi/cache+" + + "9 service/support+16 model(dto7/vo9),与 find 实测同号") + .hasSize(32); assertThat(files.stream().map(p -> p.toString().replace('\\', '/')).toList()) .as("support 目录里的既有件必须在面里——聚合入口存在的理由就是拆分后的新文件要自动被看到") .anyMatch(rel -> rel.endsWith("service/support/AppearancePatentRowNormalizer.java")); @@ -366,22 +366,23 @@ class SourceScanTest { } @Test - void recursive_包含service_support下的既有8件() { + void recursive_包含service_support下的既有9件() { List support = SourceScan.moduleSources("modules/appearancepatent").stream() .map(p -> p.toString().replace('\\', '/')) .filter(rel -> rel.contains("/service/support/")) .toList(); assertThat(support) - .as("真实集成:对真仓 modules/appearancepatent/service/support 求值,实测 8 支(3A 拆薄把归属谓词、陈旧扫描、结果打包抽成三枚 support 件,5→8)。" + .as("真实集成:对真仓 modules/appearancepatent/service/support 求值,实测 9 支(3A 拆薄把归属谓词、陈旧扫描、结果打包、LLM 批管线抽成四枚 support 件,5→9)。" + "这一格就是聚合入口存在的理由——拆分出去的新文件必须自动进面") - .hasSize(8); + .hasSize(9); assertThat(support) - .as("3A 拆出去的三枚新件必须被逐枚点名扫到:只把分母数字改大,下一回拆件又只剩「数不对」" + .as("3A 拆出去的四枚新件必须被逐枚点名扫到:只把分母数字改大,下一回拆件又只剩「数不对」" + "一记红,证不了「扫得到」这件事") .anyMatch(rel -> rel.endsWith("service/support/AppearancePatentTaskOwnership.java")) .anyMatch(rel -> rel.endsWith("service/support/AppearancePatentStaleTaskScanner.java")) - .anyMatch(rel -> rel.endsWith("service/support/AppearancePatentResultPackager.java")); + .anyMatch(rel -> rel.endsWith("service/support/AppearancePatentResultPackager.java")) + .anyMatch(rel -> rel.endsWith("service/support/AppearancePatentLlmBatchPipeline.java")); } @Test