feat(phase-4): 建单凭据需求按 task type 登记进 TaskModuleRegistry(B72-01)
`/newApi` 是整包转发的透传面(客户端只搬字节),载荷里有什么凭据需求客户端猜不出来; 铁律 27 的建单闸门此前只有两处各写一枚枚举,"这一类要什么凭据"没有一处正本。本笔把它 收成表:`TaskModuleRegistry.Module` 新增 `submitSecret`(16 类逐类登记), `requireSubmitSecret` 新增按 type 的入口,enum 形态降为包内原语——包外再想硬编一把密钥 就编译不过,只能上册表。 两处判断写死在代码里: - **登记为空是结论,不是漏填**。外观专利/货源查询要 LLM 密钥;其余几类缺的是代理, 而缺代理在执行侧是「直连降级」不是注定失败(铁律 27 边界 c),拦了会把现在能跑的功能 判死——所以本笔只登记、不新增任何拦截,`enqueue_json` 那半还等"这台机器直连能不能 采到"的实测读数(B72-01 后半欠账原样留着)。 - **空登记调到这里当场 IllegalStateException,不静默放行**。空转的闸门比没有闸门更坏, 它让下一个人以为这条链已经防住了(门禁 1 同族)。同理 `submitSecretModuleOf` 对不在 清单里的 type 抛错而不回落:「没登记」与「登记过=不需要」不许揉成同一个值。 判据:`TaskModuleCoverageTest` +3 例(登记表与实际问闸门的类型双向对撞/每个模块都 登记过/未知类型当场抛,源码扫描按 ConsoleWideningGuardrailTest 的 mainRoot 口径, 定位不到即抛不空扫)+`UserApiSecretServiceTest` +4 例(按 type 拒/放行不读库/ 空登记抛 IllegalStateException 且不碰库/未注册抛 IllegalArgumentException)。 两处快照测试跟着改到类型形态(`anyString()` 消歧重载,摘掉不再使用的 enum 导入)。 反向验证:外观专利不登记→2 红;给 COLLECT_DATA 顺手加登记→2 红;静默放行→1 红;复原复绿。 行为对现网零变化:两个调用点问的还是同一把钥匙、同一句文案。 门禁:`mvn -o test-compile` rc=0;`mvn -o test` 整量 5274/0 失败/0 错误/19 skipped BUILD SUCCESS(Phase 3 在册 5267+本笔 7)。
This commit is contained in:
+54
-17
@@ -1,8 +1,11 @@
|
||||
package com.nanri.aiimage.common.module;
|
||||
|
||||
import com.nanri.aiimage.modules.usersecret.support.UserSecretModule;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -16,6 +19,12 @@ import java.util.stream.Collectors;
|
||||
*
|
||||
* 心跳与陈旧判死两处是代码分支驱动(switch / 逐模块委派),无法纯数据派生,
|
||||
* 新增模块时仍需实现对应 Handler,测试会提示缺失。
|
||||
*
|
||||
* 建单凭据({@code submitSecret},B72-01)同样登记在本表:「这一类任务要拿用户的哪把密钥跑」
|
||||
* 的唯一出处。`/newApi` 是整包转发的透传面,客户端猜不出载荷要什么凭据,只能由服务端按 type
|
||||
* 逐类登记。这里刻意区分两件事:登记了 = 建单口必须问这把;`Optional.empty()` = **登记过的结论**
|
||||
* 「这一类建单时不需要用户凭据」,不是漏填——`TaskModuleCoverageTest` 把「问了闸门却没登记」与
|
||||
* 「登记了却没问闸门」两个方向都钉红,空壳闸门比没闸门更坏(铁律 27 边界 c)。
|
||||
*/
|
||||
public final class TaskModuleRegistry {
|
||||
|
||||
@@ -31,28 +40,34 @@ public final class TaskModuleRegistry {
|
||||
* @param selfScheduledStaleCheck 该模块的陈旧判死自带更快的调度(publish 60s / collect-data 30s),
|
||||
* 刻意不并入 2 分钟一轮的巡检线——并入会显著拉长判死时延。
|
||||
* 这类模块的"不在委托名单"是设计差异,不是漏接;由覆盖面测试固定。
|
||||
* @param submitSecret 建单口要问的那把用户密钥;{@code Optional.empty()} 是**登记过的结论**
|
||||
* "这一类不缺凭据也能跑"(例:缺代理在本机执行类是直连降级,
|
||||
* 不是注定失败——铁律 27 边界 c,拦了会把现在能跑的功能判死)。
|
||||
*/
|
||||
public record Module(String type, String label, boolean resultFileJob, boolean ageCleanup,
|
||||
boolean delegatedStaleCheck, boolean selfScheduledStaleCheck) {
|
||||
boolean delegatedStaleCheck, boolean selfScheduledStaleCheck,
|
||||
Optional<UserSecretModule> submitSecret) {
|
||||
}
|
||||
|
||||
private static final List<Module> MODULES = List.of(
|
||||
new Module("PUBLISH", "上架", true, false, false, true),
|
||||
new Module("DEDUPE", "数据去重", false, true, false, false),
|
||||
new Module("SPLIT", "数据拆分", false, true, false, false),
|
||||
new Module("CONVERT", "格式转换", false, true, false, false),
|
||||
new Module("DELETE_BRAND", "删除ASIN", true, true, false, false),
|
||||
new Module("PRODUCT_RISK_RESOLVE", "商品风险解决", true, true, false, false),
|
||||
new Module("PRICE_TRACK", "跟价", true, true, false, false),
|
||||
new Module("SHOP_MATCH", "定时匹配", true, true, false, false),
|
||||
new Module("PATROL_DELETE", "巡店删除", true, true, false, false),
|
||||
new Module("QUERY_ASIN", "查询ASIN", true, true, false, false),
|
||||
new Module("WITHDRAW", "取款", true, true, false, false),
|
||||
new Module("APPEARANCE_PATENT", "外观专利检测", true, true, true, false),
|
||||
new Module("SIMILAR_ASIN", "货源查询", true, true, true, false),
|
||||
new Module("COLLECT_DATA", "采集数据", true, true, false, true),
|
||||
new Module("SHOP_DATA_CRAWL", "店铺数据抓取", true, false, true, false),
|
||||
new Module("BRAND", "品牌检测", true, false, true, false)
|
||||
new Module("PUBLISH", "上架", true, false, false, true, Optional.empty()),
|
||||
new Module("DEDUPE", "数据去重", false, true, false, false, Optional.empty()),
|
||||
new Module("SPLIT", "数据拆分", false, true, false, false, Optional.empty()),
|
||||
new Module("CONVERT", "格式转换", false, true, false, false, Optional.empty()),
|
||||
new Module("DELETE_BRAND", "删除ASIN", true, true, false, false, Optional.empty()),
|
||||
new Module("PRODUCT_RISK_RESOLVE", "商品风险解决", true, true, false, false, Optional.empty()),
|
||||
new Module("PRICE_TRACK", "跟价", true, true, false, false, Optional.empty()),
|
||||
new Module("SHOP_MATCH", "定时匹配", true, true, false, false, Optional.empty()),
|
||||
new Module("PATROL_DELETE", "巡店删除", true, true, false, false, Optional.empty()),
|
||||
new Module("QUERY_ASIN", "查询ASIN", true, true, false, false, Optional.empty()),
|
||||
new Module("WITHDRAW", "取款", true, true, false, false, Optional.empty()),
|
||||
new Module("APPEARANCE_PATENT", "外观专利检测", true, true, true, false,
|
||||
Optional.of(UserSecretModule.APPEARANCE_PATENT)),
|
||||
new Module("SIMILAR_ASIN", "货源查询", true, true, true, false,
|
||||
Optional.of(UserSecretModule.SIMILAR_ASIN)),
|
||||
new Module("COLLECT_DATA", "采集数据", true, true, false, true, Optional.empty()),
|
||||
new Module("SHOP_DATA_CRAWL", "店铺数据抓取", true, false, true, false, Optional.empty()),
|
||||
new Module("BRAND", "品牌检测", true, false, true, false, Optional.empty())
|
||||
);
|
||||
|
||||
private TaskModuleRegistry() {
|
||||
@@ -100,6 +115,28 @@ public final class TaskModuleRegistry {
|
||||
.collect(Collectors.toUnmodifiableSet());
|
||||
}
|
||||
|
||||
/**
|
||||
* 建单口这一类任务要问的那把用户密钥(B72-01 的唯一出处)。
|
||||
*
|
||||
* @throws IllegalArgumentException moduleType 根本不在清单里(拼错的常量名)。
|
||||
* 刻意不跟 {@link #labelOf} 一样回落原值:「没登记」与「登记过=不需要」是两件事,
|
||||
* 揉成同一个空值会让调用方把漏接当成结论。
|
||||
*/
|
||||
public static Optional<UserSecretModule> submitSecretModuleOf(String moduleType) {
|
||||
for (Module module : MODULES) {
|
||||
if (module.type().equals(moduleType)) {
|
||||
return module.submitSecret();
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("模块不在清单里: " + moduleType);
|
||||
}
|
||||
|
||||
/** 登记了建单凭据的模块(覆盖面测试用它对撞「实际在建单口问闸门的类型」)。 */
|
||||
public static Set<String> submitSecretModuleTypes() {
|
||||
return MODULES.stream().filter(module -> module.submitSecret().isPresent())
|
||||
.map(Module::type).collect(Collectors.toUnmodifiableSet());
|
||||
}
|
||||
|
||||
public static String labelOf(String moduleType) {
|
||||
for (Module module : MODULES) {
|
||||
if (module.type().equals(moduleType)) {
|
||||
|
||||
+3
-2
@@ -189,10 +189,11 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
|
||||
|
||||
public AppearancePatentParseVo parseAndCreateTask(AppearancePatentParseRequest request, String deviceId) {
|
||||
// 密钥闸门排在解析之前:Excel 解析与建单四段都要花时间与配额,而缺密钥的单跑完也只是
|
||||
// 一张没有 LLM 结论的表(判据见 UserApiSecretService#requireSubmitSecret)。
|
||||
// 一张没有 LLM 结论的表(判据见 UserApiSecretService#requireSubmitSecret;
|
||||
// 这一类要哪把密钥登记在 TaskModuleRegistry,不在本文件写死)。
|
||||
requireUserId(request.getUserId());
|
||||
userApiSecretService.requireSubmitSecret(request.getUserId(),
|
||||
UserSecretModule.APPEARANCE_PATENT, request.getApiKey());
|
||||
MODULE_TYPE, request.getApiKey());
|
||||
return parsePipeline().parseAndCreateTask(request, deviceId);
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -76,7 +76,6 @@ import com.nanri.aiimage.modules.task.service.support.TaskFailureLedger;
|
||||
import com.nanri.aiimage.modules.task.service.TaskVisibleFailureNotice;
|
||||
import com.nanri.aiimage.modules.task.service.TransientPayloadStorageService;
|
||||
import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService;
|
||||
import com.nanri.aiimage.modules.usersecret.support.UserSecretModule;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -393,9 +392,10 @@ public class SimilarAsinTaskService implements SimilarAsinPipelineHost {
|
||||
throw new BusinessException("user_id 不合法");
|
||||
}
|
||||
// 密钥闸门排在解析之前:解析、建单四段都要花时间与配额,而缺密钥的单跑完只是
|
||||
// 一张没有 LLM 结论的表(判据见 UserApiSecretService#requireSubmitSecret)。
|
||||
// 一张没有 LLM 结论的表(判据见 UserApiSecretService#requireSubmitSecret;
|
||||
// 这一类要哪把密钥登记在 TaskModuleRegistry,不在本文件写死)。
|
||||
userApiSecretService.requireSubmitSecret(request.getUserId(),
|
||||
UserSecretModule.SIMILAR_ASIN, request.getApiKey());
|
||||
MODULE_TYPE, request.getApiKey());
|
||||
if (request.getFiles() == null || request.getFiles().isEmpty()) {
|
||||
throw new BusinessException("请先上传 Excel 文件");
|
||||
}
|
||||
|
||||
+23
-1
@@ -3,6 +3,7 @@ package com.nanri.aiimage.modules.usersecret.service;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.module.TaskModuleRegistry;
|
||||
import com.nanri.aiimage.common.text.SecretMasking;
|
||||
import com.nanri.aiimage.modules.admin.spi.UserSecretCleanupPort;
|
||||
import com.nanri.aiimage.common.security.ShopCredentialCryptoService;
|
||||
@@ -302,8 +303,11 @@ public class UserApiSecretService implements UserSecretCleanupPort {
|
||||
* 比一句「没配密钥」的失败更难发现。
|
||||
*
|
||||
* <p>调用方须先校验 userId(各模块自己的入口文案为准),这里不重复判。
|
||||
*
|
||||
* <p>包外不许直接调这一枚:模块→密钥的登记只认 {@link TaskModuleRegistry},
|
||||
* 跨包的入口是下面的按 type 版本(B72-01)。
|
||||
*/
|
||||
public void requireSubmitSecret(Long userId, UserSecretModule module, String callerSuppliedValue) {
|
||||
void requireSubmitSecret(Long userId, UserSecretModule module, String callerSuppliedValue) {
|
||||
if (hasText(callerSuppliedValue)) {
|
||||
return;
|
||||
}
|
||||
@@ -314,6 +318,24 @@ public class UserApiSecretService implements UserSecretCleanupPort {
|
||||
+ "缺了只会得到一张没有检测结论的结果表。请先在「密钥设置」里填好并保存。");
|
||||
}
|
||||
|
||||
/**
|
||||
* 建单前置(按 task type 问):这一类任务要拿用户的哪把密钥,唯一出处是
|
||||
* {@link TaskModuleRegistry#submitSecretModuleOf(String)}——不是调用方各写一枚枚举,
|
||||
* 更不是客户端猜({@code /newApi} 是整包转发的透传面,载荷里有什么凭据需求只有服务端按
|
||||
* type 登记才说得清,B72-01)。
|
||||
*
|
||||
* <p>登记成「这一类建单不需要用户凭据」的类型调到这里<b>当场 IllegalStateException</b>,
|
||||
* 不静默放行:空转的闸门比没有闸门更坏,它让下一个人以为这条链已经防住了。
|
||||
* 要把某类任务纳入闸门,先在被判死的形状上有实证、再上册表,不要反过来。
|
||||
*/
|
||||
public void requireSubmitSecret(Long userId, String moduleType, String callerSuppliedValue) {
|
||||
UserSecretModule module = TaskModuleRegistry.submitSecretModuleOf(moduleType)
|
||||
.orElseThrow(() -> new IllegalStateException("模块「" + moduleType
|
||||
+ "」在 TaskModuleRegistry 登记为「建单不需要用户凭据」,不该调这道闸门:"
|
||||
+ "要么把这一类真要的那把密钥登记进表,要么把闸门从这条链上摘掉"));
|
||||
requireSubmitSecret(userId, module, callerSuppliedValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 任务执行兜底读取「货源」:未配置返回空串(调用方按模块默认值处理)。
|
||||
* 代理模块额外做链接域名识别,兜住老数据(只有链接、没有供应商列)的存量行。
|
||||
|
||||
Reference in New Issue
Block a user