fix(cr-01/02/03/08/09/10/11/13): 会话世代、损耗共享基线、任务活性与工作台包

- CR-01:users.session_epoch(V172)+ JWT sessionEpoch;登录/登出原子推进,
  check_login/requireUser 先比世代再做设备比对;登出不再依赖 machine CAS
- CR-02/09:cost-guard 基线落 Redis(CostGuardBaselineStore),按 elapsed 归一化;
  非持锁实例刷新共享密钥快照;pause() 返回写成功,失败不通知且不推进基线
- CR-03:stuck_requeue_count(V173,上限 20)与 retry_count 分账;终态父任务心跳
  停止 touchRunning 并 markFailedPermanent;良性停续期 markLost=false
- CR-08:工作台包对象 key 加 UUID、0 字节拒绝、同 objectKey 幂等、同版本重复拒绝、
  前端 confirm 超时抬到 10 分钟
- CR-10:Excel onSheetTotal 统一在 onHeader 之后回调(xlsx/xls)
- CR-13/11:迁移头/文件名一致断言(V170 唯一豁免);V167 前置由 CompanyService 同事务
  复制菜单树,已应用迁移未回改
This commit is contained in:
2026-10-25 03:40:15 +08:00
parent 112c61afa8
commit c2f429f112
43 changed files with 1003 additions and 95 deletions
@@ -1,6 +1,7 @@
package com.nanri.aiimage.modules.usersecret.service;
import com.nanri.aiimage.common.service.DistributedJobLockService;
import com.nanri.aiimage.common.service.NodeSharedStore;
import com.nanri.aiimage.config.CostGuardProperties;
import com.nanri.aiimage.modules.notification.service.NotificationDispatchService;
import com.nanri.aiimage.modules.notification.service.NotificationService;
@@ -12,6 +13,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.time.Duration;
import java.time.LocalDate;
import java.util.List;
@@ -41,6 +43,7 @@ class CostGuardSchedulerTest {
private DistributedJobLockService distributedJobLockService;
private CostGuardScheduler scheduler;
private CostGuardBaselineStore baselineStore;
@BeforeEach
void setUp() {
@@ -51,8 +54,16 @@ class CostGuardSchedulerTest {
properties.setDailyProxyThreshold(100);
properties.setDailyLlmCallsThreshold(100);
properties.setDailyLlmTokensThreshold(10_000);
scheduler = new CostGuardScheduler(properties, usageMapper, guardService,
sharedSecretMonitorService, notificationDispatchService, distributedJobLockService);
// null Redis/ObjectMapper ⇒ 本地实现;两个调度器共享同一个 store 即可模拟跨实例
baselineStore = new CostGuardBaselineStore(new NodeSharedStore<>("test-cost-guard",
Duration.ofHours(1), CostGuardBaselineStore.Baseline.class, null, null, 0L));
scheduler = newScheduler(properties);
}
private CostGuardScheduler newScheduler(CostGuardProperties properties) {
return new CostGuardScheduler(properties, usageMapper, guardService,
sharedSecretMonitorService, notificationDispatchService, distributedJobLockService,
baselineStore);
}
@Test
@@ -74,6 +85,7 @@ class CostGuardSchedulerTest {
.thenReturn(List.of(row(7L, "proxy", 0, 0)))
.thenReturn(List.of(row(7L, "proxy", 12, 0)));
when(guardService.isPaused(7L)).thenReturn(false);
when(guardService.pause(anyLong(), anyString())).thenReturn(true);
scheduler.runOnce();
scheduler.runOnce();
@@ -104,6 +116,9 @@ class CostGuardSchedulerTest {
.thenReturn(List.of(row(9L, "proxy", 100, 0)))
.thenReturn(List.of(row(9L, "proxy", 101, 0)));
when(guardService.isPaused(9L)).thenReturn(false);
// 用户点继续时基线对齐到当前 100(不是删掉基线);否则首轮会按日累计立刻再暂停
when(usageMapper.sumForUser(eq(9L), any(LocalDate.class)))
.thenReturn(List.of(row(9L, "proxy", 100, 0)));
scheduler.runOnce();
scheduler.runOnce();
@@ -126,6 +141,95 @@ class CostGuardSchedulerTest {
verify(guardService, never()).pause(anyLong(), anyString());
}
@Test
void firstScanPausesWhenDailyAlreadyOverLimit() {
// 重启/首次部署后当天已经越线:首轮窗口不判,但日累计必须判,否则当天永远不会被拦
when(usageMapper.sumByUserAndModule(any(LocalDate.class)))
.thenReturn(List.of(row(7L, "proxy", 150, 0)));
when(guardService.isPaused(7L)).thenReturn(false);
when(guardService.pause(anyLong(), anyString())).thenReturn(true);
scheduler.runOnce();
verify(guardService).pause(eq(7L), contains("当日上限 100 次"));
verify(notificationDispatchService).pushToUser(eq(7L),
eq(NotificationService.SCENE_COST_GUARD), eq(NotificationService.LEVEL_ERROR),
anyString(), contains("继续任务"), isNull());
}
@Test
void pauseWriteFailureDoesNotNotify() {
when(usageMapper.sumByUserAndModule(any(LocalDate.class)))
.thenReturn(List.of(row(7L, "proxy", 0, 0)))
.thenReturn(List.of(row(7L, "proxy", 12, 0)));
when(guardService.isPaused(7L)).thenReturn(false);
when(guardService.pause(anyLong(), anyString())).thenReturn(false);
scheduler.runOnce();
scheduler.runOnce();
verify(guardService).pause(eq(7L), contains("代理提取 12 次"));
verify(notificationDispatchService, never()).pushToUser(anyLong(), anyString(), anyString(),
anyString(), anyString(), isNull());
}
@Test
void pauseWriteFailureRetriesNextScanInsteadOfSwallowingTheBreach() {
when(usageMapper.sumByUserAndModule(any(LocalDate.class)))
.thenReturn(List.of(row(7L, "proxy", 0, 0)))
.thenReturn(List.of(row(7L, "proxy", 12, 0)))
.thenReturn(List.of(row(7L, "proxy", 13, 0)));
when(guardService.isPaused(7L)).thenReturn(false);
when(guardService.pause(anyLong(), anyString())).thenReturn(false, true);
scheduler.runOnce();
scheduler.runOnce();
scheduler.runOnce();
// 第一次写失败不推进基线,所以第三轮仍能看到增量并重试暂停
verify(guardService, org.mockito.Mockito.times(2))
.pause(eq(7L), contains("代理提取"));
verify(notificationDispatchService, org.mockito.Mockito.times(1)).pushToUser(eq(7L),
eq(NotificationService.SCENE_COST_GUARD), eq(NotificationService.LEVEL_ERROR),
anyString(), contains("继续任务"), isNull());
}
@Test
void multiMinuteGapIsNormalizedToWindowInsteadOfPausingFalsely() {
// 持锁方轮换:上一轮基线是 2 分钟前,增量 360 次。按 60 秒窗口归一化是 180 次,
// 不能把 360 当成一分钟增量(那会把正常的每分钟 180 次误判成失控)。
baselineStore.put(7L, new CostGuardBaselineStore.Baseline(0, 0, 0,
System.currentTimeMillis() - 120_000L));
when(usageMapper.sumByUserAndModule(any(LocalDate.class)))
.thenReturn(List.of(row(7L, "proxy", 360, 0)));
when(guardService.isPaused(7L)).thenReturn(false);
when(guardService.pause(anyLong(), anyString())).thenReturn(true);
scheduler.runOnce();
verify(guardService).pause(eq(7L), contains("代理提取 180 次"));
}
@Test
void resumeAlignsSharedBaselineSoAnotherInstanceDoesNotRepause() {
// 实例 A 写暂停并点继续;实例 B 接手后必须读到对齐后的基线,而不是上一轮的旧值
CostGuardProperties properties = new CostGuardProperties();
properties.setProxyWindowThreshold(10);
properties.setDailyProxyThreshold(100);
CostGuardScheduler instanceB = newScheduler(properties);
when(usageMapper.sumForUser(eq(9L), any(LocalDate.class)))
.thenReturn(List.of(row(9L, "proxy", 100, 0)));
when(usageMapper.sumByUserAndModule(any(LocalDate.class)))
.thenReturn(List.of(row(9L, "proxy", 101, 0)));
when(guardService.isPaused(9L)).thenReturn(false);
scheduler.resetBaseline(9L);
instanceB.runOnce();
verify(guardService, never()).pause(anyLong(), anyString());
}
private UserSecretUsageAggregate row(long userId, String module, long calls, long tokens) {
UserSecretUsageAggregate row = new UserSecretUsageAggregate();
row.setUserId(userId);
@@ -78,6 +78,22 @@ class SharedSecretMonitorServiceTest {
anyString(), anyString(), anyString());
}
@Test
void nonHolderRefreshesSnapshotWithoutPushingDuplicateNotice() {
// 多实例下非持锁实例也要看到共享提示,但通知只由持锁方推,避免三台各推一遍
when(userApiSecretMapper.selectList(any())).thenReturn(List.of(
row(1L, "proxy", "same-cipher"),
row(2L, "proxy", "same-cipher"),
row(3L, "proxy", "same-cipher")));
service.refresh(false);
assertThat(service.warningsFor(1L)).hasSize(1);
assertThat(service.warningsFor(2L)).hasSize(1);
verify(notificationDispatchService, never()).pushToUser(anyLong(), anyString(), anyString(),
anyString(), anyString(), anyString());
}
@Test
void llmModuleIsTypedAsLlm() {
when(userApiSecretMapper.selectList(any())).thenReturn(List.of(
@@ -40,7 +40,7 @@ class UserTaskGuardServiceTest {
UserTaskGuardService service = new UserTaskGuardService(guardMapper);
when(guardMapper.selectById(7L)).thenReturn(null);
service.pause(7L, "LLM 消耗异常");
assertThat(service.pause(7L, "LLM 消耗异常")).isTrue();
verify(guardMapper).pause(7L, "LLM 消耗异常");
service.resume(7L);
@@ -56,6 +56,15 @@ class UserTaskGuardServiceTest {
assertThat(service.isPaused(0L)).isFalse();
}
@Test
void pauseWriteFailureReturnsFalseSoCallerDoesNotNotify() {
UserTaskGuardService service = new UserTaskGuardService(guardMapper);
org.mockito.Mockito.doThrow(new IllegalStateException("db down"))
.when(guardMapper).pause(7L, "LLM 消耗异常");
assertThat(service.pause(7L, "LLM 消耗异常")).isFalse();
}
@Test
void mapperFailureFallsBackToNotPaused() {
when(guardMapper.selectById(7L)).thenThrow(new IllegalStateException("db down"));