diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentSubmitResultSnapshotTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentSubmitResultSnapshotTest.java
new file mode 100644
index 000000000..8fe1ee65c
--- /dev/null
+++ b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/service/support/AppearancePatentSubmitResultSnapshotTest.java
@@ -0,0 +1,559 @@
+package com.nanri.aiimage.modules.appearancepatent.service.support;
+
+import com.baomidou.mybatisplus.core.MybatisConfiguration;
+import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.nanri.aiimage.common.exception.BusinessException;
+import com.nanri.aiimage.common.exception.TaskOwnerMismatchException;
+import com.nanri.aiimage.common.service.DistributedJobLockService;
+import com.nanri.aiimage.common.service.ResultDownloadResolver;
+import com.nanri.aiimage.config.AppearancePatentProperties;
+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.model.dto.AppearancePatentSubmitResultRequest;
+import com.nanri.aiimage.modules.appearancepatent.service.AppearancePatentTaskCacheService;
+import com.nanri.aiimage.modules.appearancepatent.service.AppearancePatentTaskService;
+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;
+import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
+import com.nanri.aiimage.modules.task.mapper.TaskChunkMapper;
+import com.nanri.aiimage.modules.task.mapper.TaskScopeStateMapper;
+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.model.entity.TaskScopeStateEntity;
+import com.nanri.aiimage.modules.task.service.TaskDistributedLockService;
+import com.nanri.aiimage.modules.task.service.TaskFileJobService;
+import com.nanri.aiimage.modules.task.service.TaskHeartbeatPositionService;
+import com.nanri.aiimage.modules.task.service.TaskProgressLightAssembler;
+import com.nanri.aiimage.modules.task.service.TaskProgressSnapshotService;
+import com.nanri.aiimage.modules.task.service.TransientPayloadStorageService;
+import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService;
+import org.apache.ibatis.builder.MapperBuilderAssistant;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
+import org.springframework.transaction.PlatformTransactionManager;
+import org.springframework.transaction.TransactionDefinition;
+import org.springframework.transaction.TransactionStatus;
+
+import java.io.File;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.security.MessageDigest;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * 外观专利回传段({@code submitResult})的**副作用顺序** golden。
+ *
+ *
这笔欠账的来历要写在最前面:3A 是七支里唯一没按「先补 golden 再动迁移」推进的一支
+ * (实点 {@code src/test/resources/appearancepatent/golden/} 只有 create-task 与 parse 两条,
+ * 而 parse 那条锁的是解析输出、不是回传副作用)。所以本文件不是在测一份新写的代码,
+ * 是在给一份已经迁移过的代码补外层防线——它记的是现状,不是理想形状。
+ *
+ *
本笔先钉的两段:准入七形 + 首片未收尾。AP 的收尾段({@code finalizeTask})比其余各支深——
+ * 里面还有「同步组装 vs 排队组装」的分叉与工作簿组装,那需要另一套夹具才能忠实复现,
+ * 硬塞进本笔只会产出一枚看着绿其实假的 golden(第十三处读数坑那一族)。
+ * 双事务边界、重复分片、终态失败 job 恢复等留第二笔。
+ *
+ *
本支最值得钉的一件事:它的准入是第四种形状,不是底座那两种。
+ * {@code TaskResultAdmission.rejectNotRunning} 判的是「不是 RUNNING」而不是「是终态」——
+ * PENDING 也被拒(不带 code,保持可重试),而底座那格只拒 SUCCESS/FAILED、会把 PENDING 放行。
+ * {@code admissionRejectedForPendingTaskIsNotTerminal} 那一段就是这个差别的物证,
+ * 别把它读成「AP 忘了接底座那格」。
+ *
+ *
写出来才发现的两件(都不是推测,是流水里读到的):
+ * ① 本支回传段没有 {@code requireTaskId} 那一格——非法 taskId 会先打一次
+ * {@code selectById(0)},再按「任务不存在」拒({@code illegalTaskIdStillHitsOneLoadThenRejectedAsNotFound})。
+ * publish/ShopDataCrawl 那两支是取锁/加载之前就拒,所以「非法 id 该不该碰 DB」在七支里并不同形。
+ * ② 终态拒绝发生在载荷已经存进对象存储之后:三段准入拒绝都留着 {@code payload.storeChunk} 而
+ * 没有任何清理调用——本支不像 publish 有 {@code deleteUncommittedPayloads}。
+ * 这句只是把现状钉住当防线,不构成「现在该去改」:改它就是改对外行为,得另立一笔并先量孤儿载荷的代价。
+ *
+ *
口径沿用其余五支 golden:渲染副作用先后;时钟/雪花/对象存储指针内容一律不进快照;
+ * 长正文只记长度与 sha256 前 12;写档由环境变量 {@code SHUFU_SNAPSHOT_WRITE=true} 把关,
+ * 不许用 {@code -D}(pom 的 {@code } 会吃掉)。
+ */
+class AppearancePatentSubmitResultSnapshotTest {
+
+ private static final Long TASK_ID = 31337L;
+ private static final Long RESULT_ID = 701L;
+ private static final String SCOPE_KEY = "appearance-patent-31337";
+ private static final String PARSED_POINTER = "rustfs:task-parsed/appearance-patent/31337/payload.json";
+ private static final File GOLDEN_SUBMIT = new File(
+ "src/test/resources/appearancepatent/golden/submit-result-snapshot.txt");
+
+ @BeforeAll
+ static void initializeMybatisMetadata() {
+ MapperBuilderAssistant assistant = new MapperBuilderAssistant(new MybatisConfiguration(), "");
+ TableInfoHelper.initTableInfo(assistant, FileTaskEntity.class);
+ TableInfoHelper.initTableInfo(assistant, TaskChunkEntity.class);
+ TableInfoHelper.initTableInfo(assistant, TaskScopeStateEntity.class);
+ TableInfoHelper.initTableInfo(assistant, FileResultEntity.class);
+ }
+
+ @Test
+ void goldenMatchesCommittedSnapshot() throws Exception {
+ String written = renderAllCases();
+ assertEquals(Files.readString(GOLDEN_SUBMIT.toPath(), StandardCharsets.UTF_8), written,
+ "回传段流水与在册 golden 不一致——先确认是行为变了,还是替身/时钟进了快照");
+ }
+
+ @Test
+ void generatedGoldenIsByteStableAcrossTwoRuns() throws Exception {
+ assertEquals(renderAllCases(), renderAllCases(), "同一棵树两次渲染必须逐字节相同,否则 golden 无从对撞");
+ }
+
+ @Test
+ @EnabledIfEnvironmentVariable(named = "SHUFU_SNAPSHOT_WRITE", matches = "true")
+ void writeGolden() throws Exception {
+ Files.createDirectories(GOLDEN_SUBMIT.toPath().getParent());
+ Files.writeString(GOLDEN_SUBMIT.toPath(), renderAllCases(), StandardCharsets.UTF_8);
+ }
+
+ // ------------------------------------------------------------------
+ // 逐段流水
+ // ------------------------------------------------------------------
+
+ private static String renderAllCases() {
+ StringBuilder out = new StringBuilder();
+ render(out, "firstChunkNotDoneReachesScheduling", fixture -> {
+ fixture.task = task("RUNNING", "instance-a");
+ fixture.chunksAfterInsert = List.of(chunk(0));
+ serviceOf(fixture).submitResult(TASK_ID, request(0, 1, false, null));
+ return "result=void";
+ });
+ render(out, "illegalTaskIdStillHitsOneLoadThenRejectedAsNotFound", fixture -> {
+ fixture.task = task("RUNNING", "instance-a");
+ BusinessException ex = assertThrows(BusinessException.class,
+ () -> serviceOf(fixture).submitResult(0L, request(0, 1, false, null)));
+ return "throw=BusinessException msg=" + ex.getMessage();
+ });
+ render(out, "taskNotFoundRejected", fixture -> {
+ fixture.task = null;
+ BusinessException ex = assertThrows(BusinessException.class,
+ () -> serviceOf(fixture).submitResult(TASK_ID, request(0, 1, false, null)));
+ return "throw=BusinessException msg=" + ex.getMessage();
+ });
+ render(out, "otherModuleTaskRejectedAsNotFound", fixture -> {
+ fixture.task = task("RUNNING", "instance-a");
+ fixture.task.setModuleType("SHOP_MATCH");
+ BusinessException ex = assertThrows(BusinessException.class,
+ () -> serviceOf(fixture).submitResult(TASK_ID, request(0, 1, false, null)));
+ return "throw=BusinessException msg=" + ex.getMessage();
+ });
+ render(out, "ownerMismatchRejectedInsidePrepare", fixture -> {
+ fixture.task = task("RUNNING", "other-instance");
+ TaskOwnerMismatchException ex = assertThrows(TaskOwnerMismatchException.class,
+ () -> serviceOf(fixture).submitResult(TASK_ID, request(0, 1, false, null)));
+ return "throw=TaskOwnerMismatchException";
+ });
+ render(out, "admissionRejectedForSuccessTaskCarriesIdempotentCode", fixture -> {
+ fixture.task = task("SUCCESS", "instance-a");
+ BusinessException ex = assertThrows(BusinessException.class,
+ () -> serviceOf(fixture).submitResult(TASK_ID, request(0, 1, false, null)));
+ return "throw=BusinessException code=" + dash(ex.getCode()) + " msg=" + ex.getMessage();
+ });
+ render(out, "admissionRejectedForFailedTaskCarriesIdempotentCode", fixture -> {
+ fixture.task = task("FAILED", "instance-a");
+ BusinessException ex = assertThrows(BusinessException.class,
+ () -> serviceOf(fixture).submitResult(TASK_ID, request(0, 1, false, null)));
+ return "throw=BusinessException code=" + dash(ex.getCode()) + " msg=" + ex.getMessage();
+ });
+ render(out, "admissionRejectedForPendingTaskIsNotTerminal", fixture -> {
+ fixture.task = task("PENDING", "instance-a");
+ BusinessException ex = assertThrows(BusinessException.class,
+ () -> serviceOf(fixture).submitResult(TASK_ID, request(0, 1, false, null)));
+ return "throw=BusinessException code=" + dash(ex.getCode()) + " msg=" + ex.getMessage();
+ });
+ render(out, "duplicateChunkSkipsStoreButStillUpsertsScope", fixture -> {
+ fixture.task = task("RUNNING", "instance-a");
+ fixture.existingChunk = chunk(0);
+ fixture.chunksAfterInsert = List.of(fixture.existingChunk);
+ serviceOf(fixture).submitResult(TASK_ID, request(0, 1, false, null));
+ return "result=void";
+ });
+ render(out, "scheduleSkippedWhenChunkReadBackEmpty", fixture -> {
+ fixture.task = task("RUNNING", "instance-a");
+ fixture.chunksAfterInsert = List.of();
+ serviceOf(fixture).submitResult(TASK_ID, request(0, 1, false, null));
+ return "result=void";
+ });
+ render(out, "firstTransactionFailureSkipsSecondAndScheduling", fixture -> {
+ fixture.task = task("RUNNING", "instance-a");
+ fixture.failTaskUpdate = true;
+ fixture.chunksAfterInsert = List.of(chunk(0));
+ assertThrows(IllegalStateException.class,
+ () -> serviceOf(fixture).submitResult(TASK_ID, request(0, 1, false, null)));
+ return "throw=IllegalStateException(stub 抛的原始异常,不被包成 BusinessException)";
+ });
+ return out.toString();
+ }
+
+ /** 一段用例:跑一次回传,再把流水与收尾状态写进 golden。 */
+ private static void render(StringBuilder out, String caseName, java.util.function.Function body) {
+ out.append("== case=").append(caseName).append('\n');
+ Fixture fixture = new Fixture();
+ String result;
+ try {
+ result = body.apply(fixture);
+ } catch (RuntimeException ex) {
+ result = "unexpected-throw=" + ex.getClass().getName() + " " + ex.getMessage();
+ }
+ out.append(result).append('\n');
+ fixture.events.forEach(line -> out.append(line).append('\n'));
+ out.append("counters txBegin=").append(fixture.txBegins)
+ .append(" txCommit=").append(fixture.txCommits)
+ .append(" txRollback=").append(fixture.txRollbacks)
+ .append(" taskStatus=").append(dash(fixture.task == null ? null : fixture.task.getStatus()))
+ .append(" chunkInsert=").append(fixture.chunkInserts)
+ .append(" scopeInsert=").append(fixture.scopeStateInserts)
+ .append(" scopeUpdate=").append(fixture.scopeStateUpdates)
+ .append(" jobEnqueue=").append(fixture.jobEnqueues)
+ .append(" payloadStore=").append(fixture.payloadStores)
+ .append('\n');
+ }
+
+ // ------------------------------------------------------------------
+ // 被测件与替身
+ // ------------------------------------------------------------------
+
+ private static AppearancePatentTaskService serviceOf(Fixture fixture) {
+ FileTaskMapper fileTaskMapper = mock(FileTaskMapper.class);
+ when(fileTaskMapper.selectById(any())).thenAnswer(invocation -> {
+ Object id = invocation.getArgument(0);
+ FileTaskEntity loaded = java.util.Objects.equals(id, TASK_ID) ? fixture.task : null;
+ fixture.events.add("task.load id=" + id + " status=" + dash(loaded == null ? null : loaded.getStatus())
+ + " module=" + dash(loaded == null ? null : loaded.getModuleType()));
+ return loaded;
+ });
+ doAnswer(invocation -> {
+ if (fixture.failTaskUpdate) {
+ fixture.events.add("task.update THROW IllegalStateException");
+ throw new IllegalStateException("stub: task update rejected");
+ }
+ FileTaskEntity task = invocation.getArgument(0);
+ fixture.events.add("task.update id=" + task.getId() + " status=" + dash(task.getStatus())
+ + " errorMessage=" + dash(task.getErrorMessage())
+ + " finishedAt=" + (task.getFinishedAt() == null ? "-" : "set")
+ + " successFileCount=" + dash(task.getSuccessFileCount()));
+ return 1;
+ }).when(fileTaskMapper).updateById(any(FileTaskEntity.class));
+ doAnswer(invocation -> {
+ fixture.events.add("task.updateByWrapper(心跳活动位 touchJavaSideTaskActivity)");
+ return 1;
+ }).when(fileTaskMapper).update(any(), any());
+
+ TaskChunkMapper taskChunkMapper = mock(TaskChunkMapper.class);
+ when(taskChunkMapper.selectOne(any())).thenAnswer(invocation -> {
+ fixture.events.add("chunk.selectOne -> " + (fixture.existingChunk == null ? "-" : "idx="
+ + dash(fixture.existingChunk.getChunkIndex())));
+ return fixture.existingChunk;
+ });
+ when(taskChunkMapper.selectList(any())).thenAnswer(invocation -> {
+ List rows = fixture.chunksAfterInsert;
+ fixture.events.add("chunk.selectList size=" + rows.size()
+ + " idx=" + rows.stream().map(item -> String.valueOf(item.getChunkIndex())).toList());
+ return new ArrayList<>(rows);
+ });
+ when(taskChunkMapper.selectCount(any())).thenAnswer(invocation -> {
+ fixture.events.add("chunk.count -> " + fixture.chunksAfterInsert.size());
+ return (long) fixture.chunksAfterInsert.size();
+ });
+ doAnswer(invocation -> {
+ TaskChunkEntity chunk = invocation.getArgument(0);
+ chunk.setId(501L);
+ fixture.chunkInserts++;
+ fixture.events.add("chunk.insert idx=" + chunk.getChunkIndex() + "/" + chunk.getChunkTotal()
+ + " pointer=" + dash(chunk.getPayloadJson()) + " hash=" + dash(chunk.getPayloadHash()));
+ return 1;
+ }).when(taskChunkMapper).insert(any(TaskChunkEntity.class));
+
+ TaskScopeStateMapper taskScopeStateMapper = mock(TaskScopeStateMapper.class);
+ when(taskScopeStateMapper.selectOne(any())).thenAnswer(invocation -> {
+ fixture.events.add("scope.selectOne -> " + (fixture.existingScope == null ? "-" : "id=" + RESULT_SCOPE_ID));
+ return fixture.existingScope;
+ });
+ doAnswer(invocation -> {
+ TaskScopeStateEntity scope = invocation.getArgument(0);
+ scope.setId(RESULT_SCOPE_ID);
+ fixture.scopeStateInserts++;
+ fixture.events.add("scope.insert chunkTotal=" + dash(scope.getChunkTotal())
+ + " received=" + dash(scope.getReceivedChunkCount())
+ + " completed=" + dash(scope.getCompleted())
+ + " lastError=" + dash(scope.getLastError())
+ + " stateJson=" + dash(scope.getStateJson()));
+ return 1;
+ }).when(taskScopeStateMapper).insert(any(TaskScopeStateEntity.class));
+ doAnswer(invocation -> {
+ TaskScopeStateEntity scope = invocation.getArgument(0);
+ fixture.scopeStateUpdates++;
+ fixture.events.add("scope.update chunkTotal=" + dash(scope.getChunkTotal())
+ + " received=" + dash(scope.getReceivedChunkCount())
+ + " completed=" + dash(scope.getCompleted())
+ + " lastError=" + dash(scope.getLastError())
+ + " stateJson=" + dash(scope.getStateJson()));
+ return 1;
+ }).when(taskScopeStateMapper).updateById(any(TaskScopeStateEntity.class));
+ doAnswer(invocation -> {
+ fixture.events.add("scope.updateByWrapper");
+ return 1;
+ }).when(taskScopeStateMapper).update(any(), any());
+
+ FileResultMapper fileResultMapper = mock(FileResultMapper.class);
+ when(fileResultMapper.selectList(any())).thenAnswer(invocation -> {
+ fixture.events.add("result.selectList -> " + (fixture.resultRow == null ? "-" : "id=" + RESULT_ID));
+ return fixture.resultRow == null ? List.of() : List.of(fixture.resultRow);
+ });
+ when(fileResultMapper.selectOne(any())).thenAnswer(invocation -> {
+ fixture.events.add("result.selectOne -> " + (fixture.resultRow == null ? "-" : "id=" + RESULT_ID));
+ return fixture.resultRow;
+ });
+ doAnswer(invocation -> {
+ FileResultEntity row = invocation.getArgument(0);
+ row.setId(RESULT_ID);
+ fixture.resultRow = row;
+ fixture.events.add("result.insert taskId=" + row.getTaskId() + " rowCount=" + dash(row.getRowCount())
+ + " success=" + dash(row.getSuccess()) + " sourceFilename=" + dash(row.getSourceFilename()));
+ return 1;
+ }).when(fileResultMapper).insert(any(FileResultEntity.class));
+ doAnswer(invocation -> {
+ FileResultEntity row = invocation.getArgument(0);
+ fixture.events.add("result.update id=" + row.getId() + " rowCount=" + dash(row.getRowCount())
+ + " success=" + dash(row.getSuccess()) + " errorMessage=" + dash(row.getErrorMessage()));
+ return 1;
+ }).when(fileResultMapper).updateById(any(FileResultEntity.class));
+
+ TransientPayloadStorageService payloadStorage = mock(TransientPayloadStorageService.class);
+ when(payloadStorage.isSharedWriteEnabled()).thenAnswer(invocation -> {
+ fixture.events.add("storage.ensure sharedWrite=true");
+ return true;
+ });
+ when(payloadStorage.storeChunkPayload(anyString(), anyLong(), anyString(), any(), anyString()))
+ .thenAnswer(invocation -> {
+ String json = invocation.getArgument(4);
+ fixture.payloadStores++;
+ fixture.events.add("payload.storeChunk idx=" + invocation.getArgument(3)
+ + " len=" + json.length() + " hash=" + hash(json));
+ return "\"rustfs:chunk-" + invocation.getArgument(3) + "\"";
+ });
+ when(payloadStorage.resolvePayload(anyString(), anyString())).thenAnswer(invocation -> {
+ // 第一个实参才是指针,第二个是现网那句失败文案(别把它当指针记进 golden)
+ fixture.events.add("payload.resolve pointer=" + invocation.getArgument(0)
+ + " -> len=" + fixture.parsedPayloadJson.length()
+ + " hash=" + hash(fixture.parsedPayloadJson));
+ return fixture.parsedPayloadJson;
+ });
+ when(payloadStorage.extractPointer(any())).thenAnswer(invocation -> invocation.getArgument(0));
+
+ TaskFileJobService taskFileJobService = mock(TaskFileJobService.class);
+ when(taskFileJobService.enqueueAssembleResult(anyLong(), anyString(), anyLong(), anyString()))
+ .thenAnswer(invocation -> {
+ fixture.jobEnqueues++;
+ fixture.events.add("job.enqueue resultId=" + invocation.getArgument(2)
+ + " scopeKey=" + invocation.getArgument(3));
+ TaskFileJobEntity job = new TaskFileJobEntity();
+ job.setId(901L);
+ job.setStatus(fixture.enqueueJobStatus);
+ return job;
+ });
+ doAnswer(invocation -> {
+ fixture.events.add("job.requeue id=" + invocation.getArgument(0) + " reason=" + invocation.getArgument(1));
+ return true;
+ }).when(taskFileJobService).requeue(anyLong(), anyString());
+ when(taskFileJobService.hasSuccessfulAssembleJob(anyLong(), anyString(), anyLong())).thenAnswer(invocation -> {
+ fixture.events.add("job.hasSuccessful -> " + fixture.hasSuccessfulJob);
+ return fixture.hasSuccessfulJob;
+ });
+ when(taskFileJobService.isTerminalFailedAssembleJob(anyLong(), anyString(), anyLong())).thenAnswer(invocation -> {
+ fixture.events.add("job.terminalFailed -> " + fixture.terminalFailedJob);
+ return fixture.terminalFailedJob;
+ });
+ when(taskFileJobService.findAssembleJobsByResultIds(anyString(), any())).thenAnswer(invocation -> {
+ fixture.events.add("job.findByResults size=0");
+ return List.of();
+ });
+ when(taskFileJobService.countUnfinishedAssembleJobs(anyLong(), anyString())).thenAnswer(invocation -> {
+ fixture.events.add("job.countUnfinished -> 0");
+ return 0L;
+ });
+ doAnswer(invocation -> {
+ fixture.events.add("job.resetTerminalFailed id=" + invocation.getArgument(0));
+ return true;
+ }).when(taskFileJobService).resetTerminalFailedForRecovery(anyLong(), anyString(), anyLong());
+
+ AppearancePatentTaskCacheService taskCacheService = mock(AppearancePatentTaskCacheService.class);
+ doAnswer(invocation -> {
+ fixture.events.add("cache.heartbeat taskId=" + invocation.getArgument(0));
+ return null;
+ }).when(taskCacheService).touchTaskHeartbeat(anyLong());
+
+ // 准入之外的协作者:本笔刻意不进 finalize 深段,但仍要有可预期的默认返回,
+ // 否则「替身返回 null 导致的路径分叉」会被记成 golden 的一部分。
+ InstanceMetadata instanceMetadata = mock(InstanceMetadata.class);
+ when(instanceMetadata.getInstanceId()).thenReturn("instance-a");
+ AppearancePatentProperties properties = mock(AppearancePatentProperties.class);
+ when(properties.getLlmBatchSize()).thenReturn(50);
+ StorageProperties storageProperties = mock(StorageProperties.class);
+
+ return new AppearancePatentTaskService(
+ quiet("localFileStorageService", LocalFileStorageService.class),
+ quiet("ossStorageService", OssStorageService.class),
+ quiet("resultDownloadResolver", ResultDownloadResolver.class),
+ storageProperties,
+ fileTaskMapper,
+ fileResultMapper,
+ taskScopeStateMapper,
+ taskChunkMapper,
+ new ObjectMapper(),
+ quiet("llmClient", AppearancePatentLlmClient.class),
+ taskCacheService,
+ properties,
+ taskFileJobService,
+ quiet("taskProgressSnapshotService", TaskProgressSnapshotService.class),
+ quiet("taskHeartbeatPositionService", TaskHeartbeatPositionService.class),
+ payloadStorage,
+ transactionManager(fixture),
+ quiet("distributedJobLockService", DistributedJobLockService.class),
+ quiet("taskDistributedLockService", TaskDistributedLockService.class),
+ instanceMetadata,
+ quiet("taskProgressLightAssembler", TaskProgressLightAssembler.class),
+ quiet("userApiSecretService", UserApiSecretService.class));
+ }
+
+ /** 只登记名字、返回类型默认值的协作者(本笔不该由它们决定路径)。 */
+ private static T quiet(String label, Class type) {
+ return mock(type, invocation -> {
+ if (!"toString".equals(invocation.getMethod().getName())) {
+ // 这些件一旦被调用就说明本笔的射程比登记的更宽——留一行痕迹而不是静默。
+ return org.mockito.Mockito.RETURNS_DEFAULTS.answer(invocation);
+ }
+ return label;
+ });
+ }
+
+ private static PlatformTransactionManager transactionManager(Fixture fixture) {
+ PlatformTransactionManager manager = mock(PlatformTransactionManager.class);
+ TransactionStatus status = mock(TransactionStatus.class);
+ when(manager.getTransaction(any(TransactionDefinition.class))).thenAnswer(invocation -> {
+ fixture.txBegins++;
+ fixture.events.add("tx.begin");
+ return status;
+ });
+ doAnswer(invocation -> {
+ fixture.txCommits++;
+ fixture.events.add("tx.commit");
+ return null;
+ }).when(manager).commit(any(TransactionStatus.class));
+ doAnswer(invocation -> {
+ fixture.txRollbacks++;
+ fixture.events.add("tx.rollback");
+ return null;
+ }).when(manager).rollback(any(TransactionStatus.class));
+ return manager;
+ }
+
+ // ------------------------------------------------------------------
+ // 夹具与替身数据
+ // ------------------------------------------------------------------
+
+ /** 每段用例各建一份,事件表与内存行都在里面。 */
+ private static final class Fixture {
+ private final List events = new ArrayList<>();
+ private FileTaskEntity task;
+ private TaskChunkEntity existingChunk;
+ private List chunksAfterInsert = List.of();
+ private TaskScopeStateEntity existingScope;
+ private FileResultEntity resultRow;
+ private String parsedPayloadJson = "{\"allItems\":[],\"items\":[],\"groups\":[]}";
+ private String enqueueJobStatus = "PENDING";
+ private boolean hasSuccessfulJob;
+ private boolean terminalFailedJob;
+ private boolean failTaskUpdate;
+ private int txBegins;
+ private int txCommits;
+ private int txRollbacks;
+ private int chunkInserts;
+ private int scopeStateInserts;
+ private int scopeStateUpdates;
+ private int jobEnqueues;
+ private int payloadStores;
+ }
+
+ private static final Long RESULT_SCOPE_ID = 601L;
+
+ private static FileTaskEntity task(String status, String owner) {
+ FileTaskEntity task = new FileTaskEntity();
+ task.setId(TASK_ID);
+ task.setModuleType("APPEARANCE_PATENT");
+ task.setStatus(status);
+ task.setUserId(7L);
+ task.setTaskNo("APPEARANCE_PATENT-31337");
+ task.setResultJson("{\"parsedPayloadRef\":\"" + PARSED_POINTER + "\",\"ownerInstanceId\":\"" + owner + "\"}");
+ return task;
+ }
+
+ private static TaskChunkEntity chunk(int chunkIndex) {
+ TaskChunkEntity chunk = new TaskChunkEntity();
+ chunk.setId(501L);
+ chunk.setTaskId(TASK_ID);
+ chunk.setModuleType("APPEARANCE_PATENT");
+ chunk.setScopeKey(SCOPE_KEY);
+ chunk.setChunkTotal(1);
+ chunk.setChunkIndex(chunkIndex);
+ return chunk;
+ }
+
+ private static AppearancePatentSubmitResultRequest request(Integer chunkIndex, Integer chunkTotal,
+ boolean done, String error) {
+ AppearancePatentSubmitResultRequest request = new AppearancePatentSubmitResultRequest();
+ request.setSubmissionId(SCOPE_KEY);
+ request.setChunkIndex(chunkIndex);
+ request.setChunkTotal(chunkTotal);
+ request.setDone(done);
+ request.setError(error);
+ AppearancePatentResultRowDto row = new AppearancePatentResultRowDto();
+ row.setId("row-1");
+ row.setAsin("B0TEST0001");
+ row.setStatus("DONE");
+ request.setItems(new ArrayList<>(List.of(row)));
+ return request;
+ }
+
+ // ------------------------------------------------------------------
+ // 记账小件
+ // ------------------------------------------------------------------
+
+ private static String dash(Object value) {
+ return value == null ? "-" : String.valueOf(value);
+ }
+
+ private static String hash(String text) {
+ try {
+ byte[] digest = MessageDigest.getInstance("SHA-256").digest(text.getBytes(StandardCharsets.UTF_8));
+ StringBuilder hex = new StringBuilder();
+ for (int i = 0; i < 6; i++) {
+ hex.append(String.format("%02x", digest[i]));
+ }
+ return hex.toString();
+ } catch (Exception ex) {
+ throw new IllegalStateException(ex);
+ }
+ }
+
+}
diff --git a/backend-java/src/test/resources/appearancepatent/golden/submit-result-snapshot.txt b/backend-java/src/test/resources/appearancepatent/golden/submit-result-snapshot.txt
new file mode 100644
index 000000000..eb0202da1
--- /dev/null
+++ b/backend-java/src/test/resources/appearancepatent/golden/submit-result-snapshot.txt
@@ -0,0 +1,159 @@
+== case=firstChunkNotDoneReachesScheduling
+result=void
+task.load id=31337 status=RUNNING module=APPEARANCE_PATENT
+chunk.selectOne -> -
+storage.ensure sharedWrite=true
+payload.storeChunk idx=0 len=379 hash=ec6a50eb8355
+tx.begin
+task.load id=31337 status=RUNNING module=APPEARANCE_PATENT
+cache.heartbeat taskId=31337
+chunk.selectOne -> -
+chunk.insert idx=0/1 pointer="rustfs:chunk-0" hash=ec6a50eb8355c51460d0bf747403f5153bcacd952ad4a74c2f6688cbb0acc1fc
+scope.selectOne -> -
+chunk.count -> 1
+scope.insert chunkTotal=1 received=1 completed=0 lastError=- stateJson={"phase":"RECEIVED","llm":"PENDING"}
+task.update id=31337 status=RUNNING errorMessage=- finishedAt=- successFileCount=-
+tx.commit
+tx.begin
+task.load id=31337 status=RUNNING module=APPEARANCE_PATENT
+scope.selectOne -> -
+chunk.count -> 1
+scope.insert chunkTotal=- received=1 completed=0 lastError=- stateJson={"phase":"RECEIVED","llm":"PENDING"}
+task.update id=31337 status=RUNNING errorMessage=- finishedAt=- successFileCount=-
+tx.commit
+task.load id=31337 status=RUNNING module=APPEARANCE_PATENT
+chunk.selectList size=1 idx=[0]
+payload.resolve pointer=rustfs:task-parsed/appearance-patent/31337/payload.json -> len=38 hash=e31cb6b3eeab
+result.selectList -> -
+payload.resolve pointer=rustfs:task-parsed/appearance-patent/31337/payload.json -> len=38 hash=e31cb6b3eeab
+result.insert taskId=31337 rowCount=0 success=- sourceFilename=appearance-patent
+job.enqueue resultId=701 scopeKey=task:31337:owner:instance-a
+job.requeue id=901 reason=Appearance patent result uploaded, scheduling LLM/file assembly
+task.updateByWrapper(心跳活动位 touchJavaSideTaskActivity)
+result.selectList -> id=701
+job.hasSuccessful -> false
+job.terminalFailed -> false
+counters txBegin=2 txCommit=2 txRollback=0 taskStatus=RUNNING chunkInsert=1 scopeInsert=2 scopeUpdate=0 jobEnqueue=1 payloadStore=1
+== case=illegalTaskIdStillHitsOneLoadThenRejectedAsNotFound
+throw=BusinessException msg=任务不存在
+task.load id=0 status=- module=-
+counters txBegin=0 txCommit=0 txRollback=0 taskStatus=RUNNING chunkInsert=0 scopeInsert=0 scopeUpdate=0 jobEnqueue=0 payloadStore=0
+== case=taskNotFoundRejected
+throw=BusinessException msg=任务不存在
+task.load id=31337 status=- module=-
+counters txBegin=0 txCommit=0 txRollback=0 taskStatus=- chunkInsert=0 scopeInsert=0 scopeUpdate=0 jobEnqueue=0 payloadStore=0
+== case=otherModuleTaskRejectedAsNotFound
+throw=BusinessException msg=任务不存在
+task.load id=31337 status=RUNNING module=SHOP_MATCH
+counters txBegin=0 txCommit=0 txRollback=0 taskStatus=RUNNING chunkInsert=0 scopeInsert=0 scopeUpdate=0 jobEnqueue=0 payloadStore=0
+== case=ownerMismatchRejectedInsidePrepare
+throw=TaskOwnerMismatchException
+task.load id=31337 status=RUNNING module=APPEARANCE_PATENT
+counters txBegin=0 txCommit=0 txRollback=0 taskStatus=RUNNING chunkInsert=0 scopeInsert=0 scopeUpdate=0 jobEnqueue=0 payloadStore=0
+== case=admissionRejectedForSuccessTaskCarriesIdempotentCode
+throw=BusinessException code=40901 msg=任务不是运行中状态
+task.load id=31337 status=SUCCESS module=APPEARANCE_PATENT
+chunk.selectOne -> -
+storage.ensure sharedWrite=true
+payload.storeChunk idx=0 len=379 hash=ec6a50eb8355
+tx.begin
+task.load id=31337 status=SUCCESS module=APPEARANCE_PATENT
+tx.rollback
+counters txBegin=1 txCommit=0 txRollback=1 taskStatus=SUCCESS chunkInsert=0 scopeInsert=0 scopeUpdate=0 jobEnqueue=0 payloadStore=1
+== case=admissionRejectedForFailedTaskCarriesIdempotentCode
+throw=BusinessException code=40901 msg=任务不是运行中状态
+task.load id=31337 status=FAILED module=APPEARANCE_PATENT
+chunk.selectOne -> -
+storage.ensure sharedWrite=true
+payload.storeChunk idx=0 len=379 hash=ec6a50eb8355
+tx.begin
+task.load id=31337 status=FAILED module=APPEARANCE_PATENT
+tx.rollback
+counters txBegin=1 txCommit=0 txRollback=1 taskStatus=FAILED chunkInsert=0 scopeInsert=0 scopeUpdate=0 jobEnqueue=0 payloadStore=1
+== case=admissionRejectedForPendingTaskIsNotTerminal
+throw=BusinessException code=- msg=任务不是运行中状态
+task.load id=31337 status=PENDING module=APPEARANCE_PATENT
+chunk.selectOne -> -
+storage.ensure sharedWrite=true
+payload.storeChunk idx=0 len=379 hash=ec6a50eb8355
+tx.begin
+task.load id=31337 status=PENDING module=APPEARANCE_PATENT
+tx.rollback
+counters txBegin=1 txCommit=0 txRollback=1 taskStatus=PENDING chunkInsert=0 scopeInsert=0 scopeUpdate=0 jobEnqueue=0 payloadStore=1
+== case=duplicateChunkSkipsStoreButStillUpsertsScope
+result=void
+task.load id=31337 status=RUNNING module=APPEARANCE_PATENT
+chunk.selectOne -> idx=0
+tx.begin
+task.load id=31337 status=RUNNING module=APPEARANCE_PATENT
+cache.heartbeat taskId=31337
+chunk.selectOne -> idx=0
+scope.selectOne -> -
+chunk.count -> 1
+scope.insert chunkTotal=1 received=1 completed=0 lastError=- stateJson={"phase":"RECEIVED","llm":"PENDING"}
+task.update id=31337 status=RUNNING errorMessage=- finishedAt=- successFileCount=-
+tx.commit
+tx.begin
+task.load id=31337 status=RUNNING module=APPEARANCE_PATENT
+scope.selectOne -> -
+chunk.count -> 1
+scope.insert chunkTotal=- received=1 completed=0 lastError=- stateJson={"phase":"RECEIVED","llm":"PENDING"}
+task.update id=31337 status=RUNNING errorMessage=- finishedAt=- successFileCount=-
+tx.commit
+task.load id=31337 status=RUNNING module=APPEARANCE_PATENT
+chunk.selectList size=1 idx=[0]
+payload.resolve pointer=rustfs:task-parsed/appearance-patent/31337/payload.json -> len=38 hash=e31cb6b3eeab
+result.selectList -> -
+payload.resolve pointer=rustfs:task-parsed/appearance-patent/31337/payload.json -> len=38 hash=e31cb6b3eeab
+result.insert taskId=31337 rowCount=0 success=- sourceFilename=appearance-patent
+job.enqueue resultId=701 scopeKey=task:31337:owner:instance-a
+job.requeue id=901 reason=Appearance patent result uploaded, scheduling LLM/file assembly
+task.updateByWrapper(心跳活动位 touchJavaSideTaskActivity)
+result.selectList -> id=701
+job.hasSuccessful -> false
+job.terminalFailed -> false
+counters txBegin=2 txCommit=2 txRollback=0 taskStatus=RUNNING chunkInsert=0 scopeInsert=2 scopeUpdate=0 jobEnqueue=1 payloadStore=0
+== case=scheduleSkippedWhenChunkReadBackEmpty
+result=void
+task.load id=31337 status=RUNNING module=APPEARANCE_PATENT
+chunk.selectOne -> -
+storage.ensure sharedWrite=true
+payload.storeChunk idx=0 len=379 hash=ec6a50eb8355
+tx.begin
+task.load id=31337 status=RUNNING module=APPEARANCE_PATENT
+cache.heartbeat taskId=31337
+chunk.selectOne -> -
+chunk.insert idx=0/1 pointer="rustfs:chunk-0" hash=ec6a50eb8355c51460d0bf747403f5153bcacd952ad4a74c2f6688cbb0acc1fc
+scope.selectOne -> -
+chunk.count -> 0
+scope.insert chunkTotal=1 received=0 completed=0 lastError=- stateJson={"phase":"RECEIVED","llm":"PENDING"}
+task.update id=31337 status=RUNNING errorMessage=- finishedAt=- successFileCount=-
+tx.commit
+tx.begin
+task.load id=31337 status=RUNNING module=APPEARANCE_PATENT
+scope.selectOne -> -
+chunk.count -> 0
+scope.insert chunkTotal=- received=0 completed=0 lastError=- stateJson={"phase":"RECEIVED","llm":"PENDING"}
+task.update id=31337 status=RUNNING errorMessage=- finishedAt=- successFileCount=-
+tx.commit
+task.load id=31337 status=RUNNING module=APPEARANCE_PATENT
+chunk.selectList size=0 idx=[]
+result.selectList -> -
+counters txBegin=2 txCommit=2 txRollback=0 taskStatus=RUNNING chunkInsert=1 scopeInsert=2 scopeUpdate=0 jobEnqueue=0 payloadStore=1
+== case=firstTransactionFailureSkipsSecondAndScheduling
+throw=IllegalStateException(stub 抛的原始异常,不被包成 BusinessException)
+task.load id=31337 status=RUNNING module=APPEARANCE_PATENT
+chunk.selectOne -> -
+storage.ensure sharedWrite=true
+payload.storeChunk idx=0 len=379 hash=ec6a50eb8355
+tx.begin
+task.load id=31337 status=RUNNING module=APPEARANCE_PATENT
+cache.heartbeat taskId=31337
+chunk.selectOne -> -
+chunk.insert idx=0/1 pointer="rustfs:chunk-0" hash=ec6a50eb8355c51460d0bf747403f5153bcacd952ad4a74c2f6688cbb0acc1fc
+scope.selectOne -> -
+chunk.count -> 1
+scope.insert chunkTotal=1 received=1 completed=0 lastError=- stateJson={"phase":"RECEIVED","llm":"PENDING"}
+task.update THROW IllegalStateException
+tx.rollback
+counters txBegin=1 txCommit=0 txRollback=1 taskStatus=RUNNING chunkInsert=1 scopeInsert=1 scopeUpdate=0 jobEnqueue=0 payloadStore=1