fix(文案出口): 用户可见提示语一律走 UserMessages 把关,并补结果作业租约围栏

两件事同源:2026-10-09 任务 30456 的卡片上同时出现中文进度和一句英文异常原文。

围栏:派发链在执行前重读 job 行,让 activateRunningClaim 的 CAS 恒真,撞锁方
deferRunning 把在跑的行翻回 PENDING,markSuccess 静默写 0 行——成功被判成失败。
claim 快照现在贯穿到 runClaimed 不再回读,markSuccess 返回布尔并补 stage=SUCCESS_LOST
日志,让这次丢失可观测。

文案:普查数出 61 处把 ex.getMessage() 拼进用户可见字段(异常响应、任务 error_message、
结果作业 error_msg、看板 message),逐条翻译追不上新增速度,改为出口把关——
UserMessages.with(中文主干, 异常) 只拼接中文详情、orFallback(原文, 兜底句) 只放行含中文的串,
被换掉的原文进日志;GlobalExceptionHandler 按码分档兜底(401/4011/40902 各说各的话)。
另把 chunk-read-failed[N] 摘要写成"第 N 分片读取失败"、TASK_LOCK_BUSY 换成中文(40902 码不动,
客户端按码分支)。数字人两处与结果作业 worker 故意仍存原文:孤儿判定与三条 preserves raw
用例按 error_message 读它,界面侧由前端 userNotice 兜住,已在新尺子里点名豁免并反查豁免是否还被用到。

尺子:RawExceptionMessageSinkGuardTest(出口行数下限硬钉 1500+内存造违例自证)、UserMessagesTest、
UserFacingMessageChineseGuardTest 豁免表收到只剩 JSON 键 msg 一条。mvn -o test 4205 例 0 失败。
This commit is contained in:
2026-10-09 19:34:25 +08:00
parent 6325a63b52
commit 2a4d15b801
76 changed files with 1160 additions and 279 deletions
@@ -0,0 +1,223 @@
package com.nanri.aiimage;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
/**
* 全仓尺子:凡是会进用户界面的文案出口({@code new BusinessException(...)} 与
* {@code saveFileBuildProgress(...)}),首段必须是中文。
*
* <p>起因是 2026-10-09 一张货源查询任务截图:同一个卡片上既有中文"仅完成 102/112 行",
* 又有一句英文 "Similar ASIN result rows are empty; please retry result generation"。
* 那次拆分只翻译了 legacy 装配路径,bounded 路径的 8 句英文异常原样进了
* {@code biz_task_file_job.error_message},前端 {@code pendingResultHint} 又不加转换地渲染。
* 全仓普查当时数出 1,213 个带字面量的出口、其中 112 个首段没有中文。
*
* <p>判据是"首段字面量含中文",不是"每段都含中文":拼接里的单位片段
* ({@code " bytes"}、{@code " 分钟)"})本就不该有中文。
*
* <p>豁免表必须条条被用到——用不到的豁免就是常开的洞,所以 {@link #noDeadExemptions} 专门反查。
*/
class UserFacingMessageChineseGuardTest {
/** 扫描到的出口总数下限:低于此值说明抽取器什么都没抓到,那时的"零违规"是假的。 */
private static final int MIN_OUTLETS_SCANNED = 1200;
/**
* 逐条点名豁免,格式 {@code 文件后缀 :: 首段字面量}。
* 每一条都要在下面写清为什么可以留英文,以及什么时候该摘掉。
*/
private static final List<String> EXEMPTIONS = List.of(
// JSON 键名,不是消息:真正的兜底文案在同一个参数列表的最后一段,已是中文
"modules/imagevideo/service/ImageVideoCozeService.java::msg");
@Test
void everyUserFacingMessageOutletStartsWithChinese() throws IOException {
List<String> offenders = new ArrayList<>();
Set<String> usedExemptions = new LinkedHashSet<>();
int scanned = 0;
for (Path file : mainSources()) {
String source = read(file);
String relative = relativeName(file);
for (String arguments : messageArguments(source)) {
String first = firstLiteral(arguments);
if (first.isBlank()) {
continue;
}
scanned++;
if (containsChinese(first)) {
continue;
}
String key = relative + "::" + first;
if (EXEMPTIONS.contains(key)) {
usedExemptions.add(key);
continue;
}
offenders.add(relative + " -> \"" + first + "\"");
}
}
assertThatListIsEmpty(offenders, "这些文案出口的首段不是中文,会原样出现在用户界面上");
assertTrue(scanned >= MIN_OUTLETS_SCANNED,
"只扫到 " + scanned + " 个带字面量的出口(下限 " + MIN_OUTLETS_SCANNED
+ ")——抽取器失效了,此刻的\"零违规\"不作数");
}
/** 用不到的豁免要摘掉,否则它会慢慢变成一个"谁都能往里塞一条"的常开门。 */
@Test
void noDeadExemptions() throws IOException {
Set<String> seen = new LinkedHashSet<>();
for (Path file : mainSources()) {
String relative = relativeName(file);
for (String arguments : messageArguments(read(file))) {
String first = firstLiteral(arguments);
if (!first.isBlank()) {
seen.add(relative + "::" + first);
}
}
}
List<String> dead = EXEMPTIONS.stream().filter(key -> !seen.contains(key)).toList();
assertThatListIsEmpty(dead, "这些豁免已经没有对应的英文出口了,请从 EXEMPTIONS 里删掉");
}
private static void assertThatListIsEmpty(List<String> items, String message) {
if (!items.isEmpty()) {
fail(message + ":\n " + String.join("\n ", items));
}
}
private static final List<String> MARKERS = List.of("new BusinessException(", "saveFileBuildProgress(");
/** 每个标记后面那段配平的括号内容,就是文案所在的地方。 */
private static List<String> messageArguments(String source) {
List<String> arguments = new ArrayList<>();
for (String marker : MARKERS) {
int from = 0;
while (true) {
int at = source.indexOf(marker, from);
if (at < 0) {
break;
}
from = at + marker.length();
int open = source.indexOf('(', at);
String body = balancedArguments(source, open);
if (body == null) {
fail("括号没配平,扫描器会在 " + marker + " 处漏判(offset=" + at + ")");
}
arguments.add(body);
}
}
return arguments;
}
private static String balancedArguments(String source, int openParen) {
int depth = 0;
boolean inString = false;
for (int i = openParen; i < source.length(); i++) {
char c = source.charAt(i);
if (inString) {
if (c == '\\') {
i++;
} else if (c == '"') {
inString = false;
}
continue;
}
if (c == '"') {
inString = true;
} else if (c == '(') {
depth++;
} else if (c == ')') {
depth--;
if (depth == 0) {
return source.substring(openParen + 1, i);
}
}
}
return null;
}
private static String firstLiteral(String arguments) {
int start = -1;
boolean inString = false;
for (int i = 0; i < arguments.length(); i++) {
char c = arguments.charAt(i);
if (inString) {
if (c == '\\') {
i++;
continue;
}
if (c == '"') {
return arguments.substring(start + 1, i);
}
continue;
}
if (c == '"') {
inString = true;
start = i;
}
}
return "";
}
private static boolean containsChinese(String text) {
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c >= 0x4E00 && c <= 0x9FFF) {
return true;
}
}
return false;
}
/** 相对 src/main/java/com/nanri/aiimage 的路径,豁免表用它点名,行号会漂所以不进键。 */
private static String relativeName(Path file) {
String path = file.toString().replace('\\', '/');
int cut = path.indexOf("/com/nanri/aiimage/");
return cut < 0 ? path : path.substring(cut + "/com/nanri/aiimage/".length());
}
private static List<Path> mainSources() throws IOException {
Path root = moduleRoot().resolve("src/main/java");
if (!Files.isDirectory(root)) {
fail("找不到 " + root + " —— 用例依赖以 backend-java 为工作目录运行,别让守卫静默通过");
}
try (Stream<Path> walk = Files.walk(root)) {
return walk.filter(Files::isRegularFile)
.filter(p -> p.getFileName().toString().endsWith(".java"))
.collect(Collectors.toList());
}
}
/** surefire 的工作目录是模块目录;从仓库根或 IDE 跑时退到 backend-java/。 */
private static Path moduleRoot() {
Path here = Path.of("").toAbsolutePath();
if (Files.isDirectory(here.resolve("src/main/java"))) {
return here;
}
Path nested = here.resolve("backend-java");
if (Files.isDirectory(nested.resolve("src/main/java"))) {
return nested;
}
throw new IllegalStateException("无法定位 backend-java 模块目录(cwd=" + here + ")");
}
private static String read(Path file) {
try {
return Files.readString(file, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new IllegalStateException("读取 " + file + " 失败", e);
}
}
}