Compare commits

...

3 Commits

Author SHA1 Message Date
huangzd1997 1dd9e66d21 fix(cr-02): CostGuardBaselineStore 公开构造器补 @Autowired
真机拉起后端时 Spring 在双构造器下找不到默认构造器:
No default constructor found。补 @Autowired 指定注入构造器;
仅装配修复,不改共享基线语义。
2026-10-25 03:45:36 +08:00
huangzd1997 c2f429f112 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 同事务
  复制菜单树,已应用迁移未回改
2026-10-25 03:40:15 +08:00
huangzd1997 112c61afa8 feat(brandgating): 品牌Gating库并入店铺数据重复检查页
- 「店铺数据重复检查」新增「品牌Gating库」页签,承接原独立 Gating 网页后台的列表/筛选/统计、单条新增、批量导入、启停与删除。
- 复用既有 /api/admin/brand-gating 模块;接口权限键跟随宿主页 admin_shop_data_duplicate_check(V164 菜单已由 V166 删除)。
- 前端新增 brand-gating 模型/API 适配、BrandGatingPanel 与纯逻辑单测。
2026-10-25 02:09:07 +08:00
50 changed files with 2173 additions and 116 deletions
@@ -61,8 +61,11 @@ export async function confirmWorkbenchClientPackage(
fileName: string,
version: string,
): Promise<WorkbenchClientPackageItem | null> {
// confirm 要服务端流式复算最多 512MB 的 sha256;默认 30s 会在慢 MinIO/跨地域回源时
// 先超时,而服务端可能已经落库——管理员重试会得到「已登记」的错位(CR-08)。
const { data } = await http.post<unknown>(WORKBENCH_CLIENT_CONFIRM_ENDPOINT, null, {
params: { object_key: objectKey, file_name: fileName, version },
timeout: 600_000,
})
return parseWorkbenchClientPackageUpload(data)
}
@@ -94,5 +97,11 @@ export async function uploadWorkbenchClientPackage(
onProgress(total > 0 ? Math.min(Math.round((event.loaded / total) * 100), 100) : 0)
},
})
return confirmWorkbenchClientPackage(target.objectKey, target.fileName, target.version)
try {
return await confirmWorkbenchClientPackage(target.objectKey, target.fileName, target.version)
} catch (error) {
// 对象已经 PUT 成功,confirm 失败不等于上传失败;引导管理员先刷新列表,避免重复上传
const reason = error instanceof Error ? error.message : String(error)
throw new Error(`安装包已上传,但确认未完成(${reason})。请先刷新列表确认,不要立即重复上传。`)
}
}
@@ -0,0 +1,787 @@
<script setup lang="ts">
/**
* 品牌Gating库管理面板:以第三个页签嵌在「店铺数据重复检查」页里。
* 数据源 /api/admin/brand-gating(biz_brand_gating,全平台共享,不随公司 Tab 收窄)。
*/
import { computed, onMounted, reactive, ref } from 'vue'
import { formatDateTime } from '@/utils/datetime'
import { actionableErrorText, type AdminFeedbackKind } from '@/components/admin-feedback'
import { showAdminFeedback } from '@/components/admin-feedback-ui'
import { openAdminConfirm } from '@/components/admin-confirm'
import OldPagination from '@/components/OldPagination.vue'
import LoadFailureBar from '@/components/LoadFailureBar.vue'
import {
GATING_COUNTRIES,
GATING_SOURCES,
GATING_TYPES,
createBrandGatingFilter,
createBrandGatingForm,
validateBrandGatingForm,
type BrandGatingFilter,
type BrandGatingForm,
type BrandGatingRow,
type BrandGatingStats,
} from './brand-gating-model.ts'
import {
deleteBrandGating,
fetchBrandGatingPage,
fetchBrandGatingStats,
importBrandGatingText,
submitBrandGating,
updateBrandGatingStatus,
} from './brand-gating-api.ts'
const filter = reactive<BrandGatingFilter>(createBrandGatingFilter())
const page = ref(1)
const pageSize = ref(10)
const rows = ref<BrandGatingRow[]>([])
const total = ref(0)
const pageLoading = ref(false)
const loadError = ref('')
const loaded = ref(false)
const stats = ref<BrandGatingStats | null>(null)
const statsLoading = ref(false)
const statsError = ref('')
const form = reactive<BrandGatingForm>(createBrandGatingForm())
const addBusy = ref(false)
const batchText = ref('')
const importBusy = ref(false)
const busyRowId = ref(0)
const tipVisible = ref(true)
const countryOptions = GATING_COUNTRIES.filter((country) => country !== '全部')
const typeOptions = GATING_TYPES
const statsCards = computed(() => {
const current = stats.value
const byType = new Map((current?.byType ?? []).map((item) => [item.name, item.count]))
return [
{ label: 'Gating总记录', value: current?.total ?? 0, tone: '' },
{ label: '生效中', value: current?.active ?? 0, tone: 'ok' },
{ label: '已解除', value: current?.disabled ?? 0, tone: 'muted' },
{ label: '去重品牌数', value: current?.brands ?? 0, tone: '' },
{ label: '品牌gating', value: byType.get('品牌gating') ?? 0, tone: 'primary' },
{ label: '类目gating', value: byType.get('类目gating') ?? 0, tone: 'warn' },
{ label: '账户不符合', value: byType.get('账户不符合') ?? 0, tone: 'danger' },
]
})
function feedback(message: string, kind: AdminFeedbackKind): void {
showAdminFeedback(message, kind)
}
async function loadStats(): Promise<void> {
statsLoading.value = true
statsError.value = ''
try {
stats.value = await fetchBrandGatingStats()
} catch (error) {
statsError.value = actionableErrorText(error)
} finally {
statsLoading.value = false
}
}
async function loadPage(nextPage = page.value): Promise<void> {
pageLoading.value = true
loadError.value = ''
try {
const result = await fetchBrandGatingPage(filter, nextPage, pageSize.value)
rows.value = result.rows
total.value = result.total
page.value = nextPage
loaded.value = true
} catch (error) {
loadError.value = actionableErrorText(error)
} finally {
pageLoading.value = false
}
}
function query(): void {
loadPage(1)
}
function resetFilter(): void {
Object.assign(filter, createBrandGatingFilter())
loadPage(1)
}
function changePageSize(size: number): void {
pageSize.value = size
loadPage(1)
}
async function addOne(): Promise<void> {
const error = validateBrandGatingForm(form)
if (error) {
feedback(error, 'warning')
return
}
addBusy.value = true
try {
const added = await submitBrandGating(form)
if (added) {
feedback('新增成功', 'success')
form.brand = ''
form.asin = ''
form.remark = ''
} else {
feedback('该品牌 + 国家已存在,未重复添加', 'warning')
}
await Promise.all([loadPage(added ? 1 : page.value), loadStats()])
} catch (error) {
feedback(actionableErrorText(error), 'error')
} finally {
addBusy.value = false
}
}
async function batchAdd(): Promise<void> {
const text = batchText.value.trim()
if (!text) {
feedback('请粘贴批量数据', 'warning')
return
}
importBusy.value = true
try {
const result = await importBrandGatingText(text)
feedback(`批量导入完成:新增 ${result.added} 条,跳过重复 ${result.skipped} 条`, 'success')
batchText.value = ''
await Promise.all([loadPage(1), loadStats()])
} catch (error) {
feedback(actionableErrorText(error), 'error')
} finally {
importBusy.value = false
}
}
async function toggleStatus(row: BrandGatingRow): Promise<void> {
const enabling = row.status !== 1
if (!enabling) {
const confirmed = await openAdminConfirm({ danger: false,
title: '解除Gating',
message: `确认解除「${row.brand}」的Gating记录?解除后排查会跳过该品牌。`,
confirmText: '确认解除',
})
if (!confirmed) return
}
busyRowId.value = row.id
try {
await updateBrandGatingStatus(row.id, enabling ? 1 : 0)
feedback(enabling ? '已恢复生效' : '已解除,排查不再拦截', 'success')
await Promise.all([loadPage(), loadStats()])
} catch (error) {
feedback(actionableErrorText(error), 'error')
} finally {
busyRowId.value = 0
}
}
async function removeRow(row: BrandGatingRow): Promise<void> {
const confirmed = await openAdminConfirm({ danger: true,
targetName: row.brand,
message: `确认删除 ID=${row.id} 的「${row.brand}」Gating记录?此操作可能影响已有数据。`,
})
if (!confirmed) return
busyRowId.value = row.id
try {
await deleteBrandGating(row.id)
feedback('已删除', 'success')
const nextPage = rows.value.length === 1 && page.value > 1 ? page.value - 1 : page.value
await Promise.all([loadPage(nextPage), loadStats()])
} catch (error) {
feedback(actionableErrorText(error), 'error')
} finally {
busyRowId.value = 0
}
}
function typeClass(type: string): string {
if (type.includes('账户')) return 'g-tag-acc'
if (type.includes('类目')) return 'g-tag-cat'
return 'g-tag-brand'
}
function sourceText(source: string): string {
if (source === 'AUTO') return '自动同步'
if (source === 'MANUAL') return '手动'
return source || '—'
}
onMounted(() => {
loadStats()
loadPage(1)
})
</script>
<template>
<div class="gating-panel">
<div v-if="tipVisible" class="g-tip">
<span class="g-tip-title">当前页面操作提示</span>
<span>
维护不符合规则的品牌(品牌Gating / 类目Gating)。新增后可用下方筛选快速回查;云端 Gating 库与前端排查软件同步同一份数据,命中后会自动拦截。
<b>Gating 库为全平台共享数据,不随上方公司页签切换。</b>
</span>
<button class="g-tip-x" type="button" @click="tipVisible = false">收起 ✕</button>
</div>
<div v-if="statsError" class="g-stats-error">
<span>统计加载失败:{{ statsError }}</span>
<button type="button" @click="loadStats">重试</button>
</div>
<section class="g-cards" v-loading="statsLoading">
<div v-for="card in statsCards" :key="card.label" class="g-card" :class="card.tone">
<div class="g-n">{{ card.value.toLocaleString() }}</div>
<div class="g-t">{{ card.label }}</div>
</div>
</section>
<div class="g-forms">
<section class="g-panel">
<div class="g-panel-head">新增 Gating 品牌</div>
<div class="g-panel-body">
<div class="g-fields">
<label class="g-field">
<span>品牌 *</span>
<input v-model="form.brand" placeholder="品牌(必填)" @keyup.enter="addOne" />
</label>
<label class="g-field">
<span>国家</span>
<select v-model="form.country">
<option v-for="country in GATING_COUNTRIES" :key="country" :value="country">{{ country }}</option>
</select>
</label>
<label class="g-field">
<span>类型</span>
<select v-model="form.gatingType">
<option v-for="type in typeOptions" :key="type" :value="type">{{ type }}</option>
</select>
</label>
<label class="g-field">
<span>ASIN</span>
<input v-model="form.asin" placeholder="ASIN(可选)" @keyup.enter="addOne" />
</label>
<label class="g-field g-field-wide">
<span>备注</span>
<input v-model="form.remark" placeholder="备注(可选)" @keyup.enter="addOne" />
</label>
</div>
<div class="g-form-actions">
<button class="g-btn g-btn-primary" type="button" :disabled="addBusy" @click="addOne">
{{ addBusy ? '新增中…' : '新增' }}
</button>
</div>
</div>
</section>
<section class="g-panel">
<div class="g-panel-head">批量导入 Gating 品牌</div>
<div class="g-panel-body">
<textarea
v-model="batchText"
class="g-textarea"
placeholder="每行一个,格式:品牌,国家,类型,ASIN,备注(后四列可省略)&#10;示例:&#10;tory burch,英国,品牌gating&#10;jellycat,全部,品牌gating&#10;crocs,德国,类目gating"
></textarea>
<div class="g-form-actions">
<button class="g-btn g-btn-violet" type="button" :disabled="importBusy" @click="batchAdd">
{{ importBusy ? '导入中…' : '批量导入' }}
</button>
</div>
</div>
</section>
</div>
<section class="g-panel">
<div class="g-panel-head">Gating 品牌列表</div>
<div class="g-panel-body">
<div class="g-filters">
<input v-model="filter.keyword" placeholder="搜索品牌 / ASIN" @keyup.enter="query" />
<select v-model="filter.country">
<option value="">全部国家</option>
<option v-for="country in countryOptions" :key="country" :value="country">{{ country }}</option>
</select>
<select v-model="filter.gatingType">
<option value="">全部类型</option>
<option v-for="type in typeOptions" :key="type" :value="type">{{ type }}</option>
</select>
<select v-model="filter.status">
<option value="">全部状态</option>
<option value="1">生效</option>
<option value="0">已解除</option>
</select>
<select v-model="filter.source">
<option value="">全部来源</option>
<option v-for="source in GATING_SOURCES" :key="source" :value="source">{{ sourceText(source) }}</option>
</select>
<button class="g-btn g-btn-primary" type="button" @click="query">查询</button>
<button class="g-btn g-btn-ghost" type="button" @click="resetFilter">重置</button>
</div>
<LoadFailureBar v-if="loadError" :message="loadError" @retry="loadPage()" />
<div class="g-table-wrap" v-loading="pageLoading">
<table v-if="rows.length" class="g-table">
<thead>
<tr>
<th style="width: 64px">ID</th>
<th>品牌</th>
<th>ASIN</th>
<th style="width: 82px">国家</th>
<th style="width: 112px">类型</th>
<th style="width: 82px">状态</th>
<th style="width: 100px">来源</th>
<th>店铺</th>
<th>备注</th>
<th style="width: 156px">创建时间</th>
<th style="width: 118px">操作</th>
</tr>
</thead>
<tbody>
<tr v-for="row in rows" :key="row.id">
<td class="g-dim">{{ row.id }}</td>
<td class="g-strong">{{ row.brand }}</td>
<td class="g-mono">{{ row.asin || '—' }}</td>
<td>{{ row.country || '—' }}</td>
<td><span class="g-tag" :class="typeClass(row.gatingType)">{{ row.gatingType || '—' }}</span></td>
<td>
<span class="g-tag" :class="row.status === 1 ? 'g-tag-on' : 'g-tag-off'">
{{ row.status === 1 ? '生效' : '已解除' }}
</span>
</td>
<td :class="row.source === 'AUTO' ? 'g-source-auto' : 'g-source-manual'">{{ sourceText(row.source) }}</td>
<td>{{ row.shopName || '—' }}</td>
<td class="g-remark" :title="row.remark">{{ row.remark || '—' }}</td>
<td class="g-time">{{ formatDateTime(row.createdAt) }}</td>
<td>
<div class="g-ops">
<button
class="g-btn g-btn-sm"
:class="row.status === 1 ? 'g-btn-ghost' : 'g-btn-green'"
type="button"
:disabled="busyRowId === row.id"
@click="toggleStatus(row)"
>
{{ row.status === 1 ? '解除' : '恢复' }}
</button>
<button
class="g-btn g-btn-sm g-btn-danger"
type="button"
:disabled="busyRowId === row.id"
@click="removeRow(row)"
>
删除
</button>
</div>
</td>
</tr>
</tbody>
</table>
<div v-else-if="!pageLoading && loaded" class="g-empty">暂无数据,请先在上方新增或使用前端排查软件同步</div>
</div>
<OldPagination
v-if="total"
:total="total"
:page="page"
:page-size="pageSize"
@change="loadPage"
@size-change="changePageSize"
/>
</div>
</section>
</div>
</template>
<style scoped>
.gating-panel {
--g-border: #e5e9f0;
--g-text: #1c2230;
--g-text2: #5a6474;
--g-text3: #98a1b1;
--g-primary: #2e5be6;
--g-danger: #d63a4a;
--g-green: #1f9d62;
--g-amber: #d9912b;
--g-violet: #7c5cd6;
--g-surface: #ffffff;
--g-mono: "Cascadia Mono", "SF Mono", Consolas, "Courier New", monospace;
--g-shadow: 0 1px 2px rgba(20, 30, 60, 0.05), 0 4px 18px rgba(20, 30, 60, 0.06);
display: flex;
flex-direction: column;
gap: 14px;
}
.g-tip {
display: flex;
align-items: flex-start;
gap: 10px;
background: #f5f9ff;
border: 1px solid #cfe0fb;
border-left: 4px solid var(--g-primary);
border-radius: 10px;
padding: 10px 14px;
font-size: 12px;
color: #1e40af;
line-height: 1.7;
}
.g-tip-title {
flex: none;
font-weight: 700;
color: #1d4ed8;
}
.g-tip b {
color: #1d4ed8;
}
.g-tip-x {
flex: none;
margin-left: auto;
border: none;
background: none;
color: #6b9ef0;
font-family: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
}
.g-stats-error {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 9px 14px;
border-radius: 9px;
border: 1px solid #f3c6cc;
background: #fdebed;
color: var(--g-danger);
font-size: 12px;
}
.g-stats-error button {
border: none;
background: none;
color: var(--g-danger);
font-family: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
}
.g-cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(132px, 1fr));
gap: 10px;
}
.g-card {
background: var(--g-surface);
border: 1px solid var(--g-border);
border-top: 3px solid #9ca3af;
border-radius: 10px;
box-shadow: var(--g-shadow);
padding: 12px 14px;
}
.g-card.ok { border-top-color: var(--g-green); }
.g-card.muted { border-top-color: #b8c0ce; }
.g-card.primary { border-top-color: var(--g-primary); }
.g-card.warn { border-top-color: var(--g-amber); }
.g-card.danger { border-top-color: var(--g-danger); }
.g-card .g-n {
font-size: 24px;
font-weight: 700;
color: var(--g-text);
font-variant-numeric: tabular-nums;
line-height: 1.2;
}
.g-card .g-t {
margin-top: 3px;
font-size: 11.5px;
color: var(--g-text3);
}
.g-forms {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
gap: 14px;
}
.g-panel {
background: var(--g-surface);
border: 1px solid var(--g-border);
border-radius: 10px;
box-shadow: var(--g-shadow);
overflow: hidden;
}
.g-panel-head {
padding: 11px 15px;
background: #fafbfd;
border-bottom: 1px solid #e9edf3;
font-size: 13px;
font-weight: 700;
color: var(--g-text);
}
.g-panel-head::before {
content: "";
display: inline-block;
width: 7px;
height: 7px;
border-radius: 2px;
background: #2dd4bf;
margin-right: 8px;
vertical-align: 1px;
}
.g-panel-body {
padding: 14px 15px;
}
.g-fields {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 10px 12px;
}
.g-field {
display: flex;
flex-direction: column;
gap: 5px;
min-width: 0;
}
.g-field > span {
font-size: 12px;
color: var(--g-text2);
}
.g-field-wide {
grid-column: span 2;
}
.g-field input,
.g-field select,
.g-filters input,
.g-filters select {
width: 100%;
height: 34px;
padding: 0 10px;
border: 1px solid #d7dce5;
border-radius: 7px;
background: #fbfcfe;
color: var(--g-text);
font-family: inherit;
font-size: 13px;
outline: none;
}
.g-field input:focus,
.g-field select:focus,
.g-filters input:focus,
.g-filters select:focus {
border-color: var(--g-primary);
box-shadow: 0 0 0 3px rgba(46, 91, 230, 0.12);
background: #fff;
}
.g-form-actions {
display: flex;
justify-content: flex-end;
margin-top: 12px;
}
.g-textarea {
width: 100%;
height: 126px;
padding: 9px 10px;
border: 1px solid #d7dce5;
border-radius: 7px;
background: #fbfcfe;
color: var(--g-text);
font-family: inherit;
font-size: 12.5px;
line-height: 1.6;
resize: vertical;
outline: none;
}
.g-textarea:focus {
border-color: var(--g-primary);
box-shadow: 0 0 0 3px rgba(46, 91, 230, 0.12);
background: #fff;
}
.g-filters {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
margin-bottom: 12px;
}
.g-filters input {
width: 190px;
}
.g-filters select {
width: 132px;
}
.g-btn {
height: 34px;
padding: 0 15px;
border: 1px solid transparent;
border-radius: 7px;
font-family: inherit;
font-size: 13px;
font-weight: 600;
cursor: pointer;
transition: background 0.15s, border-color 0.15s, color 0.15s;
}
.g-btn:disabled {
opacity: 0.55;
cursor: not-allowed;
}
.g-btn-sm {
height: 28px;
padding: 0 10px;
font-size: 12px;
border-radius: 6px;
}
.g-btn-primary {
background: var(--g-primary);
border-color: var(--g-primary);
color: #fff;
}
.g-btn-primary:hover:not(:disabled) {
background: #1e4bd8;
border-color: #1e4bd8;
}
.g-btn-violet {
background: var(--g-violet);
border-color: var(--g-violet);
color: #fff;
}
.g-btn-violet:hover:not(:disabled) {
background: #6b4bc4;
border-color: #6b4bc4;
}
.g-btn-green {
background: #e7f7ef;
border-color: #bce7d2;
color: var(--g-green);
}
.g-btn-green:hover:not(:disabled) {
background: #d9f2e5;
}
.g-btn-danger {
background: #fdebed;
border-color: #f3c6cc;
color: var(--g-danger);
}
.g-btn-danger:hover:not(:disabled) {
background: #fbdde1;
}
.g-btn-ghost {
background: #fff;
border-color: #d7dce5;
color: var(--g-text2);
}
.g-btn-ghost:hover:not(:disabled) {
border-color: var(--g-primary);
color: var(--g-primary);
}
.g-table-wrap {
overflow-x: auto;
border: 1px solid var(--g-border);
border-radius: 9px;
min-height: 90px;
}
.g-table {
width: 100%;
border-collapse: separate;
border-spacing: 0;
font-size: 12.5px;
}
.g-table th {
position: sticky;
top: 0;
z-index: 1;
padding: 10px;
background: #f7f9fc;
border-bottom: 1px solid var(--g-border);
color: var(--g-text2);
font-size: 12px;
font-weight: 600;
text-align: left;
white-space: nowrap;
}
.g-table td {
padding: 9px 10px;
border-bottom: 1px solid #eff2f7;
color: var(--g-text);
white-space: nowrap;
}
.g-table tbody tr:hover td {
background: #f7faff;
}
.g-table tbody tr:last-child td {
border-bottom: none;
}
.g-strong {
font-weight: 600;
}
.g-dim {
color: var(--g-text3);
}
.g-mono {
font-family: var(--g-mono);
}
.g-time {
color: var(--g-text2);
font-size: 11.5px;
}
.g-remark {
max-width: 190px;
overflow: hidden;
text-overflow: ellipsis;
color: var(--g-text2);
}
.g-source-auto {
color: var(--g-violet);
}
.g-source-manual {
color: #0d9488;
}
.g-tag {
display: inline-block;
padding: 2px 8px;
border-radius: 6px;
font-size: 11px;
font-weight: 600;
white-space: nowrap;
}
.g-tag-brand {
background: #eff6ff;
color: #2563eb;
}
.g-tag-cat {
background: #fef3c7;
color: #b45309;
}
.g-tag-acc {
background: #fee2e2;
color: #dc2626;
}
.g-tag-on {
background: #d1fae5;
color: #10b981;
}
.g-tag-off {
background: #e5e7eb;
color: #9ca3af;
}
.g-ops {
display: flex;
flex-wrap: wrap;
gap: 6px;
}
.g-empty {
padding: 42px 16px;
text-align: center;
color: var(--g-text3);
font-size: 13px;
}
</style>
@@ -13,8 +13,9 @@ import { aggregateDrawerRows, drawerSummaryOf, dupWithinGroup, indexShopGroups,
import type { DuplicateConsole, LedgerRow } from './duplicate-console-model.ts'
import { ASIN_COUNTRY_CODES, asinCountryLabel } from '../asin/asin-country.ts'
import { orderedPair } from '@/utils/datetime'
import BrandGatingPanel from './BrandGatingPanel.vue'
type Tab = 'dup' | 'ledger'
type Tab = 'dup' | 'ledger' | 'gating'
type Scope = 'global' | 'group'
const consoleData = ref<DuplicateConsole | null>(null)
@@ -431,18 +432,18 @@ onMounted(() => {
<div class="dup-head">
<div>
<h2>店铺数据撞款监控</h2>
<p>按 ASIN×店铺×站点分析同一 ASIN 在多家自营店铺/分组内的重复上架;分组归属来自店铺管理,只读展示。</p>
<p>按 ASIN×店铺×站点分析同一 ASIN 在多家自营店铺/分组内的重复上架;分组归属来自店铺管理,只读展示。品牌Gating库在同一页的第三个页签维护。</p>
</div>
</div>
<div v-if="loadError" class="load-banner">
<div v-if="loadError && activeTab !== 'gating'" class="load-banner">
<span>{{ loadError }}</span>
<button class="btn btn-ghost btn-sm" type="button" @click="loadConsole">重试</button>
<button class="btn btn-ghost btn-sm" type="button" @click="loadError = ''">知道了</button>
</div>
<!-- 筛选条件 -->
<section class="card filter">
<!-- 筛选条件(撞款/台账专用,Gating 页签隐藏) -->
<section v-if="activeTab !== 'gating'" class="card filter">
<div class="filter-title">筛选条件</div>
<div class="filter-row">
<div class="f-group">
@@ -492,10 +493,9 @@ onMounted(() => {
</div>
</section>
<div v-loading="loading" class="dup-body-wrap">
<template v-if="consoleData">
<div v-loading="loading && activeTab !== 'gating'" class="dup-body-wrap">
<!-- KPI 统计 -->
<section class="kpis">
<section v-if="consoleData && activeTab !== 'gating'" class="kpis">
<div v-for="kpi in kpis" :key="kpi.label" class="kpi" :class="kpi.tone">
<div class="k-label"><span class="k-dot"></span>{{ kpi.label }}</div>
<div class="k-value">{{ kpi.value }}</div>
@@ -504,7 +504,7 @@ onMounted(() => {
</section>
<!-- 店铺上架分布 -->
<section class="card store-strip">
<section v-if="consoleData && activeTab !== 'gating'" class="card store-strip">
<div class="strip-head">
<span class="strip-title">店铺上架分布</span>
<span class="strip-note">各店铺在本批站点中的上架记录量(含重复扫描)</span>
@@ -527,8 +527,9 @@ onMounted(() => {
<div class="tabs">
<button class="tab" :class="{ active: activeTab === 'dup' }" type="button" @click="switchTab('dup')">撞款监控 <span class="t-count">{{ totalDupCount }}</span></button>
<button class="tab" :class="{ active: activeTab === 'ledger' }" type="button" @click="switchTab('ledger')">全部ASIN台账 <span class="t-count">{{ ledgerTotal }}</span></button>
<button class="tab" :class="{ active: activeTab === 'gating' }" type="button" @click="switchTab('gating')">品牌Gating库</button>
</div>
<div class="actions">
<div v-if="activeTab !== 'gating'" class="actions">
<span v-if="overview" class="updated">数据更新时间 {{ overview.scannedAt || '—' }}</span>
<button v-if="isSuper" class="btn btn-ghost btn-sm" type="button" @click="groupInfoVisible = true">
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2" /><circle cx="9" cy="7" r="4" /><path d="M23 21v-2a4 4 0 0 0-3-3.87" /><path d="M16 3.13a4 4 0 0 1 0 7.75" /></svg>
@@ -545,8 +546,13 @@ onMounted(() => {
</div>
</div>
<!-- ============ 品牌Gating库(全平台共享数据) ============ -->
<template v-if="activeTab === 'gating'">
<BrandGatingPanel />
</template>
<!-- ============ 撞款监控 ============ -->
<template v-if="activeTab === 'dup'">
<template v-else-if="consoleData && activeTab === 'dup'">
<div v-if="isSuper" class="scope-bar">
<div class="seg">
<button class="seg-btn" :class="{ active: scope === 'global' }" type="button" @click="switchScope('global')">全店撞款</button>
@@ -626,7 +632,7 @@ onMounted(() => {
</template>
<!-- ============ 全部ASIN台账 ============ -->
<template v-else>
<template v-else-if="consoleData">
<div class="table-wrap" v-loading="ledgerLoading">
<table v-if="ledgerItems.length" class="ledger">
<thead>
@@ -661,9 +667,8 @@ onMounted(() => {
</div>
<OldPagination v-if="ledgerTotal" :total="ledgerTotal" :page="ledgerPage" :page-size="ledgerPageSize" @change="goPage" @size-change="changeLedgerSize" />
</template>
</template>
<div v-else-if="!loadError" class="card empty-state">
<div v-if="!consoleData && activeTab !== 'gating' && !loadError" class="card empty-state">
<div class="e-title">暂无扫描结果</div>
请点击右上角「重新分析」触发撞款扫描
</div>
@@ -0,0 +1,60 @@
/** 品牌Gating库接口适配:/api/admin/brand-gating 的列表/统计/新增/启停/删除/批量导入。 */
import { http } from '@/api/http'
import { unwrap } from '@/api/envelope'
import {
parseBrandGatingAddResult,
parseBrandGatingImportResult,
parseBrandGatingPage,
parseBrandGatingStats,
toBrandGatingQuery,
type BrandGatingFilter,
type BrandGatingForm,
type BrandGatingImportResult,
type BrandGatingPage,
type BrandGatingStats,
} from './brand-gating-model.ts'
export const BRAND_GATING_ENDPOINT = '/api/admin/brand-gating'
export async function fetchBrandGatingPage(
filter: BrandGatingFilter,
page: number,
pageSize: number,
): Promise<BrandGatingPage> {
const { data } = await http.get<unknown>(BRAND_GATING_ENDPOINT, {
params: toBrandGatingQuery(filter, page, pageSize),
})
return parseBrandGatingPage(unwrap<unknown>(data))
}
export async function fetchBrandGatingStats(): Promise<BrandGatingStats> {
const { data } = await http.get<unknown>(`${BRAND_GATING_ENDPOINT}/stats`)
return parseBrandGatingStats(unwrap<unknown>(data))
}
/** 单条新增:true=新增成功,false=品牌+国家已存在(后端撞唯一键返回 added=0)。 */
export async function submitBrandGating(form: BrandGatingForm): Promise<boolean> {
const { data } = await http.post<unknown>(BRAND_GATING_ENDPOINT, {
brand: form.brand.trim(),
country: form.country,
gatingType: form.gatingType,
asin: form.asin.trim(),
remark: form.remark.trim(),
})
return parseBrandGatingAddResult(unwrap<unknown>(data))
}
export async function updateBrandGatingStatus(id: number, status: 0 | 1): Promise<void> {
const { data } = await http.post<unknown>(`${BRAND_GATING_ENDPOINT}/${id}/status`, { status })
unwrap<unknown>(data)
}
export async function deleteBrandGating(id: number): Promise<void> {
const { data } = await http.delete<unknown>(`${BRAND_GATING_ENDPOINT}/${id}`)
unwrap<unknown>(data)
}
export async function importBrandGatingText(text: string): Promise<BrandGatingImportResult> {
const { data } = await http.post<unknown>(`${BRAND_GATING_ENDPOINT}/import`, { text })
return parseBrandGatingImportResult(unwrap<unknown>(data))
}
@@ -0,0 +1,177 @@
/** 品牌Gating库模型(任务:并入「店铺数据重复检查」页签):与 /api/admin/brand-gating 的 camel 契约对齐,纯逻辑。 */
export const GATING_COUNTRIES = ['全部', '英国', '德国', '法国', '西班牙', '意大利'] as const
export const GATING_TYPES = ['品牌gating', '类目gating', '账户不符合'] as const
export const GATING_SOURCES = ['AUTO', 'MANUAL'] as const
export type GatingStatusFilter = '' | '1' | '0'
export interface BrandGatingFilter {
keyword: string
country: string
gatingType: string
status: GatingStatusFilter
source: string
}
export interface BrandGatingForm {
brand: string
country: string
gatingType: string
asin: string
remark: string
}
export interface BrandGatingRow {
id: number
brand: string
asin: string
country: string
gatingType: string
/** 1=生效 0=已解除。 */
status: number
source: string
shopName: string
remark: string
createdAt: string
updatedAt: string
disabledAt: string
}
export interface BrandGatingNameCount {
name: string
count: number
}
export interface BrandGatingStats {
total: number
active: number
disabled: number
brands: number
byType: BrandGatingNameCount[]
byCountry: BrandGatingNameCount[]
bySource: BrandGatingNameCount[]
}
export interface BrandGatingPage {
total: number
rows: BrandGatingRow[]
}
export interface BrandGatingImportResult {
added: number
skipped: number
}
export function createBrandGatingFilter(): BrandGatingFilter {
return { keyword: '', country: '', gatingType: '', status: '', source: '' }
}
export function createBrandGatingForm(): BrandGatingForm {
return { brand: '', country: '全部', gatingType: '品牌gating', asin: '', remark: '' }
}
/** 列表查询参数:空值与「全部」哨兵不下发,其余原样交给后端(与桌面版 list_gating 同口径)。 */
export function toBrandGatingQuery(
filter: BrandGatingFilter,
page: number,
pageSize: number,
): Record<string, string | number> {
const query: Record<string, string | number> = {
page: Math.max(1, Math.trunc(page) || 1),
size: Math.max(1, Math.trunc(pageSize) || 10),
}
const keyword = filter.keyword.trim()
if (keyword) query.keyword = keyword
if (filter.country && filter.country !== '全部') query.country = filter.country
if (filter.gatingType && filter.gatingType !== '全部') query.gatingType = filter.gatingType
if (filter.status === '1' || filter.status === '0') query.status = filter.status
if (filter.source && filter.source !== '全部') query.source = filter.source
return query
}
/** 单条新增前置校验:只拦空品牌,其余字段由后端落桌面版同款默认值。 */
export function validateBrandGatingForm(form: BrandGatingForm): string {
if (!form.brand.trim()) return '请填写品牌'
return ''
}
function asRecord(value: unknown): Record<string, unknown> | null {
return value && typeof value === 'object' ? (value as Record<string, unknown>) : null
}
function textOf(value: unknown): string {
return value == null ? '' : String(value).trim()
}
function countOf(value: unknown): number {
const count = Number(value)
return Number.isFinite(count) ? Math.max(0, Math.trunc(count)) : 0
}
function nameCountsOf(value: unknown): BrandGatingNameCount[] {
if (!Array.isArray(value)) return []
return value
.map((item) => {
const record = asRecord(item)
if (!record) return null
const name = textOf(record.name)
return name ? { name, count: countOf(record.count) } : null
})
.filter((item): item is BrandGatingNameCount => item !== null)
}
export function parseBrandGatingRow(raw: unknown): BrandGatingRow | null {
const record = asRecord(raw)
if (!record) return null
const id = Number(record.id)
if (!Number.isFinite(id)) return null
return {
id,
brand: textOf(record.brand),
asin: textOf(record.asin),
country: textOf(record.country),
gatingType: textOf(record.gatingType),
status: countOf(record.status) === 1 ? 1 : 0,
source: textOf(record.source),
shopName: textOf(record.shopName),
remark: textOf(record.remark),
createdAt: textOf(record.createdAt),
updatedAt: textOf(record.updatedAt),
disabledAt: textOf(record.disabledAt),
}
}
export function parseBrandGatingPage(raw: unknown): BrandGatingPage {
const record = asRecord(raw)
if (!record) return { total: 0, rows: [] }
const rows = Array.isArray(record.rows)
? record.rows
.map(parseBrandGatingRow)
.filter((row): row is BrandGatingRow => row !== null)
: []
return { total: countOf(record.total), rows }
}
export function parseBrandGatingStats(raw: unknown): BrandGatingStats {
const record = asRecord(raw)
return {
total: countOf(record?.total),
active: countOf(record?.active),
disabled: countOf(record?.disabled),
brands: countOf(record?.brands),
byType: nameCountsOf(record?.byType),
byCountry: nameCountsOf(record?.byCountry),
bySource: nameCountsOf(record?.bySource),
}
}
/** 单条新增回包:added=1 新增成功,0 表示撞唯一键(品牌+国家已存在)。 */
export function parseBrandGatingAddResult(raw: unknown): boolean {
return countOf(asRecord(raw)?.added) > 0
}
export function parseBrandGatingImportResult(raw: unknown): BrandGatingImportResult {
const record = asRecord(raw)
return { added: countOf(record?.added), skipped: countOf(record?.skipped) }
}
@@ -0,0 +1,117 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import {
createBrandGatingFilter,
createBrandGatingForm,
parseBrandGatingAddResult,
parseBrandGatingImportResult,
parseBrandGatingPage,
parseBrandGatingStats,
toBrandGatingQuery,
validateBrandGatingForm,
} from '../src/pages/tasks/brand-gating-model.ts'
test('brand_gating_form_defaults_match_desktop', () => {
const form = createBrandGatingForm()
assert.equal(form.country, '全部')
assert.equal(form.gatingType, '品牌gating')
assert.equal(form.brand, '')
assert.equal(validateBrandGatingForm(form), '请填写品牌')
assert.equal(validateBrandGatingForm({ ...form, brand: ' tory burch ' }), '')
})
test('brand_gating_query_omits_empty_and_all_sentinel', () => {
const filter = createBrandGatingFilter()
assert.deepEqual(toBrandGatingQuery(filter, 1, 10), { page: 1, size: 10 })
filter.country = '全部'
filter.gatingType = '全部'
filter.source = '全部'
assert.deepEqual(toBrandGatingQuery(filter, 2, 20), { page: 2, size: 20 })
filter.keyword = ' tory '
filter.country = '英国'
filter.gatingType = '类目gating'
filter.status = '1'
filter.source = 'AUTO'
assert.deepEqual(toBrandGatingQuery(filter, 3, 50), {
page: 3,
size: 50,
keyword: 'tory',
country: '英国',
gatingType: '类目gating',
status: '1',
source: 'AUTO',
})
})
test('brand_gating_page_parses_camel_rows_and_skips_invalid', () => {
const page = parseBrandGatingPage({
total: 2,
rows: [
{
id: 7,
brand: 'tory burch',
asin: 'B000000001',
country: '英国',
gatingType: '品牌gating',
status: 1,
source: 'MANUAL',
shopName: 'ShopA',
remark: '排查同步',
createdAt: '2026-10-24 10:00:00',
updatedAt: '2026-10-24 10:00:00',
disabledAt: null,
},
{ brand: '缺 id' },
],
})
assert.equal(page.total, 2)
assert.equal(page.rows.length, 1)
const row = page.rows[0]
assert.equal(row.id, 7)
assert.equal(row.gatingType, '品牌gating')
assert.equal(row.status, 1)
assert.equal(row.disabledAt, '')
assert.equal(row.createdAt, '2026-10-24 10:00:00')
})
test('brand_gating_stats_parses_groups_and_missing_fields', () => {
const stats = parseBrandGatingStats({
total: 9,
active: 6,
disabled: 3,
brands: 5,
byType: [
{ name: '品牌gating', count: 4 },
{ name: '', count: 99 },
{ count: 1 },
],
byCountry: [{ name: '英国', count: 3 }],
})
assert.equal(stats.total, 9)
assert.equal(stats.active, 6)
assert.equal(stats.brands, 5)
assert.deepEqual(stats.byType, [{ name: '品牌gating', count: 4 }])
assert.deepEqual(stats.byCountry, [{ name: '英国', count: 3 }])
assert.deepEqual(stats.bySource, [])
const empty = parseBrandGatingStats(null)
assert.deepEqual(empty, {
total: 0,
active: 0,
disabled: 0,
brands: 0,
byType: [],
byCountry: [],
bySource: [],
})
})
test('brand_gating_add_and_import_results', () => {
assert.equal(parseBrandGatingAddResult({ added: 1 }), true)
assert.equal(parseBrandGatingAddResult({ added: 0 }), false)
assert.equal(parseBrandGatingAddResult(null), false)
assert.deepEqual(parseBrandGatingImportResult({ added: 3, skipped: 2 }), { added: 3, skipped: 2 })
assert.deepEqual(parseBrandGatingImportResult(null), { added: 0, skipped: 0 })
})
@@ -126,16 +126,13 @@ public final class ExcelStreamReader {
SheetCollector collector = new SheetCollector(sheetName, sheetNo, handler, recorder);
XSSFSheetXMLHandler sheetHandler =
new XSSFSheetXMLHandler(styles, sharedStrings, collector, formatter, false);
DimensionFilter filter = new DimensionFilter(parser, maxRows -> {
try {
handler.onSheetTotal(sheetName, sheetNo, maxRows);
} catch (Exception ex) {
throw recorder.record(ex);
}
});
// dimension 在 sheetData 之前出现,先缓存;等 onHeader 回调之后再对外发 onSheetTotal,
// 保持旧 EasyExcel「先表头、后总行数」的顺序(CR-10)。
DimensionFilter filter = new DimensionFilter(parser, collector::setApproximateTotalRows);
filter.setContentHandler(sheetHandler);
parser.setContentHandler(filter);
parser.parse(new InputSource(sheet));
collector.reportTotalIfNeeded();
}
private static void readHssf(InputStream inputStream, boolean readAllSheets, SheetRowHandler handler)
@@ -152,11 +149,12 @@ public final class ExcelStreamReader {
final String sheetName = sheet.getSheetName();
final int currentSheetNo = sheetNo;
final int lastRow = sheet.getLastRowNum();
callRecorder(recorder, () -> handler.onSheetTotal(sheetName, currentSheetNo, lastRow + 1));
final Map<Integer, String> header = rowToMap(sheet.getRow(0), formatter);
if (!header.isEmpty()) {
callRecorder(recorder, () -> handler.onHeader(sheetName, currentSheetNo, header));
}
// 旧顺序是「表头 → 总行数」;xls 路径此前反了,统一回来(CR-10)
callRecorder(recorder, () -> handler.onSheetTotal(sheetName, currentSheetNo, lastRow + 1));
for (int rowIndex = 1; rowIndex <= lastRow; rowIndex++) {
Row row = sheet.getRow(rowIndex);
if (row == null) {
@@ -274,6 +272,9 @@ public final class ExcelStreamReader {
private final FailureRecorder recorder;
private Map<Integer, String> header = Map.of();
private Map<Integer, String> row = new LinkedHashMap<>();
private Integer approximateTotalRows;
private boolean headerReported;
private boolean totalReported;
private SheetCollector(String sheetName, int sheetNo, SheetRowHandler handler, FailureRecorder recorder) {
this.sheetName = sheetName;
@@ -282,6 +283,21 @@ public final class ExcelStreamReader {
this.recorder = recorder;
}
/** dimension 过滤器的回调:只缓存,等 onHeader 之后才对外发。 */
private void setApproximateTotalRows(Integer rows) {
this.approximateTotalRows = rows;
reportTotalIfNeeded();
}
private void reportTotalIfNeeded() {
if (!headerReported || totalReported) {
return;
}
totalReported = true;
callRecorder(recorder,
() -> handler.onSheetTotal(sheetName, sheetNo, approximateTotalRows));
}
@Override
public void startRow(int rowNum) {
row = new LinkedHashMap<>();
@@ -294,7 +310,9 @@ public final class ExcelStreamReader {
}
if (rowNum == 0) {
header = new LinkedHashMap<>(row);
headerReported = true;
callRecorder(recorder, () -> handler.onHeader(sheetName, sheetNo, header));
reportTotalIfNeeded();
} else {
Map<Integer, String> values = new LinkedHashMap<>(row);
int rowIndex = rowNum;
@@ -49,4 +49,10 @@ public class AdminUserEntity {
@TableField(value = "updated_at", insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER)
private LocalDateTime updatedAt;
private String machine;
/**
* 会话世代(V172 新增):登录/登出各 +1,JWT 的 epoch claim 必须与它相等。
* 与 {@code LoginUserEntity.sessionEpoch} 同列,改一处必须同改两处。
*/
@TableField("session_epoch")
private Long sessionEpoch;
}
@@ -72,6 +72,9 @@ public class AdminAuthSupport {
// 吊销先于单设备比对:那条路径第一行就是 if (exempt) return;(超管豁免),
// 把吊销挂在它下面等于超管永不见吊销
DeviceSessionPolicy.assertNotRevoked(user.getMachine(), user.getId(), user.getUsername());
// 世代校验同样先于单设备比对,且不受 singleDevice 开关支配:登出/新登录后旧令牌一律失效
DeviceSessionPolicy.assertSessionEpoch(user.getSessionEpoch(),
DeviceSessionPolicy.claimSessionEpoch(claims), user.getId(), user.getUsername());
// 单设备登录:被新设备顶下线的旧 token 在此统一拦截(全站 requireUser 调用点自动生效)
if (authProperties.isSingleDeviceEnabled()) {
DeviceSessionPolicy.assertSameDevice(user.getMachine(), DeviceSessionPolicy.claimDeviceId(claims),
@@ -91,6 +91,30 @@ public final class DeviceSessionPolicy {
return raw == null ? "" : raw.toString().trim();
}
/**
* 从 JWT claims 中提取会话世代;缺失(本次升级前签发的旧令牌)按 0 读取,
* 与 {@code users.session_epoch} 的列默认值一致,升级不会统一踢掉存量会话。
*/
public static long claimSessionEpoch(Claims claims) {
Object raw = claims == null ? null : claims.get("sessionEpoch");
// JwtService 签发的是 Number;非 Number 一律视同缺失(按 0),不在这里做字符串兼容,
// 避免为一种不会出现的 claim 形状新增 catch(CatchSilenceRatchet 满格)。
return raw instanceof Number number ? number.longValue() : 0L;
}
/**
* 会话世代必须与库中值相等。登出与每次新登录都会 +1,因此登出前签发的令牌在
* 下一次请求即失效;不依赖 {@code machine} 上的 revoked@ 标记,也不受单设备开关支配。
*/
public static void assertSessionEpoch(Long storedEpoch, long claimEpoch, Long userId, String username) {
long current = storedEpoch == null ? 0L : storedEpoch;
if (current != claimEpoch) {
log.warn("[auth] 会话世代失效被拒 userId={} username={} stored={} claim={}",
userId, username, current, claimEpoch);
throw new BusinessException(401, "登录已过期,请重新登录");
}
}
/**
* 校验请求携带的 token 是否仍属于账号当前绑定的设备。
* 超管豁免;machine 为空(尚未绑定)放行;不匹配抛 4011。
@@ -85,13 +85,19 @@ public class JwtService {
return key;
}
public String issue(Long userId, String username, String deviceId) {
/**
* 签发令牌。{@code sessionEpoch} 必须取自 users.session_epoch(登录/登出各推进一次),
* 鉴权时与库中值比对;旧令牌没有这个 claim 时按 0 读取,见
* {@link DeviceSessionPolicy#claimSessionEpoch(Claims)}。
*/
public String issue(Long userId, String username, String deviceId, long sessionEpoch) {
Instant now = Instant.now();
Instant exp = now.plus(Duration.ofHours(props.getJwtTtlHours()));
return Jwts.builder()
.subject(String.valueOf(userId))
.claim("username", username)
.claim("deviceId", deviceId)
.claim("sessionEpoch", sessionEpoch)
.issuedAt(Date.from(now))
.expiration(Date.from(exp))
.signWith(signingKey())
@@ -74,6 +74,22 @@ public final class NodeSharedStore<K, V> {
return remote;
}
/**
* 远端优先;远端没有才回退本地。用于「锁在实例间轮换、新持锁方必须读到上一持锁方
* 写入的基线」这类状态——{@link #get} 的本地优先会让每个实例 forever 读自己的旧值。
*/
public V getFresh(K key) {
if (key == null) {
return null;
}
V remote = readRemote(key);
if (remote != null) {
local.put(key, remote);
return remote;
}
return local.get(key);
}
public boolean containsKey(K key) {
return get(key) != null;
}
@@ -3,7 +3,14 @@ package com.nanri.aiimage.modules.auth.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.auth.model.entity.LoginUserEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface LoginUserMapper extends BaseMapper<LoginUserEntity> {
/**
* 会话世代 +1(登录、登出各调用一次)。原子自增而不是「读出来再写回」:
* 双节点同时登录时后者不能把前者的世代覆盖回去。返回 0 行表示用户已不存在。
*/
int incrementSessionEpoch(@Param("userId") Long userId);
}
@@ -43,4 +43,10 @@ public class LoginUserEntity {
*/
@TableField("disabled_at")
private LocalDateTime disabledAt;
/**
* 会话世代(V172 新增):登录/登出各 +1,JWT 的 epoch claim 必须与它相等。
* 与 {@code AdminUserEntity.sessionEpoch} 同列,改一处必须同改两处。
*/
@TableField("session_epoch")
private Long sessionEpoch;
}
@@ -116,6 +116,7 @@ public class AuthService {
accountExpiryService.assertLoginAllowed(user);
boolean isAdmin = user.getIsAdmin() != null && user.getIsAdmin() == 1;
advanceSessionEpoch(user);
// 单设备登录:登录成功即把账号绑定到当前设备(last-login-wins),原设备下一次请求被顶下线
if (authProperties.isSingleDeviceEnabled()) {
DeviceSessionPolicy.logBindOnLogin(user.getMachine(), deviceId, user.getId(), user.getUsername());
@@ -125,7 +126,8 @@ public class AuthService {
} else if (DeviceSessionPolicy.isRevoked(user.getMachine())) {
// 开关关掉后登录不再写 machine,登出留下的吊销标记就没人清:此后每次登录都会先成功、
// 再被 requireUser 的 assertNotRevoked 判成「登录已过期」,账号永久锁死。
// 登录成功即视为新会话,清空标记(空=未绑定,放行语义);仍不写设备号。
// 登录成功即视为新会话(上面已推进 session_epoch,旧令牌另有世代校验兜底),
// 清空标记(空=未绑定,放行语义);仍不写设备号。
log.warn("[auth] 登录清除登出吊销标记 userId={} username={}", user.getId(), user.getUsername());
loginUserMapper.update(null, new LambdaUpdateWrapper<LoginUserEntity>()
.eq(LoginUserEntity::getId, user.getId())
@@ -149,6 +151,9 @@ public class AuthService {
if (userId == null) {
return;
}
// 世代 +1 是真正的吊销;它不依赖 machine 是否等于本次 deviceId,
// 因此 machine 为空(单设备关闭时的常态)也一定留得下吊销效果。
int epochUpdated = loginUserMapper.incrementSessionEpoch(userId);
String marker = DeviceSessionPolicy.REVOKED_PREFIX + userId + "#"
+ java.time.LocalDateTime.now().format(REVOKE_STAMP);
String bound = deviceId == null ? "" : deviceId.trim();
@@ -156,7 +161,8 @@ public class AuthService {
.set(LoginUserEntity::getMachine, marker)
.eq(LoginUserEntity::getId, userId)
.eq(!bound.isEmpty(), LoginUserEntity::getMachine, bound));
log.info("[auth] 登出吊销 userId={} deviceBound={} updated={}", userId, bound, updated);
log.info("[auth] 登出吊销 userId={} deviceBound={} epochUpdated={} markerUpdated={}",
userId, bound, epochUpdated, updated);
}
/** 从令牌里取出身份再吊销;解析不出(已过期/伪造)就什么都不做——cookie 由调用方清。 */
@@ -173,7 +179,20 @@ public class AuthService {
log.warn("[auth] 登出时 subject 非数字,跳过吊销(cookie 仍由调用方清)");
return;
}
logout(Long.parseLong(subject), DeviceSessionPolicy.claimDeviceId(claims));
Long userId = Long.parseLong(subject);
LoginUserEntity current = loginUserMapper.selectById(userId);
if (current == null) {
return;
}
// 旧令牌不能把新会话一起登出:只有当前世代的令牌才有权推进世代。
long claimEpoch = DeviceSessionPolicy.claimSessionEpoch(claims);
long storedEpoch = current.getSessionEpoch() == null ? 0L : current.getSessionEpoch();
if (claimEpoch != storedEpoch) {
log.warn("[auth] 登出令牌的会话世代已过期,跳过吊销 userId={} stored={} claim={}",
userId, storedEpoch, claimEpoch);
return;
}
logout(userId, DeviceSessionPolicy.claimDeviceId(claims));
}
public LoginResultVo checkLogin(String token, String currentDeviceId) {
@@ -196,6 +215,9 @@ public class AuthService {
}
boolean isAdmin = user.getIsAdmin() != null && user.getIsAdmin() == 1;
String claimDeviceId = DeviceSessionPolicy.claimDeviceId(claims);
// 世代校验先于设备比对:登出/新登录后旧 token 在这里直接 401,不能靠 check_login 续期复活
DeviceSessionPolicy.assertSessionEpoch(user.getSessionEpoch(),
DeviceSessionPolicy.claimSessionEpoch(claims), user.getId(), user.getUsername());
// 单设备登录:不拦截的话,被顶下线的旧 token 会在这里续期「复活」
if (authProperties.isSingleDeviceEnabled()) {
DeviceSessionPolicy.assertSameDevice(user.getMachine(), claimDeviceId,
@@ -238,8 +260,20 @@ public class AuthService {
return authProperties.getCookieName();
}
/** 登录推进世代;返回 0 行说明用户行已消失,不能再签发一个「世代比库还新」的令牌。 */
private void advanceSessionEpoch(LoginUserEntity user) {
int updated = loginUserMapper.incrementSessionEpoch(user.getId());
if (updated != 1) {
log.warn("[auth] 登录推进会话世代失败 userId={} updated={}", user.getId(), updated);
throw new BusinessException(500, "登录状态更新失败,请重试");
}
long current = user.getSessionEpoch() == null ? 0L : user.getSessionEpoch();
user.setSessionEpoch(current + 1);
}
private LoginResultVo buildResult(LoginUserEntity user, String deviceId, boolean isAdmin) {
String token = jwtService.issue(user.getId(), user.getUsername(), deviceId);
String token = jwtService.issue(user.getId(), user.getUsername(), deviceId,
user.getSessionEpoch() == null ? 0L : user.getSessionEpoch());
LoginResultVo vo = new LoginResultVo();
vo.setUserId(user.getId());
vo.setUsername(user.getUsername());
@@ -33,8 +33,10 @@ import org.springframework.web.bind.annotation.RestController;
* 品牌Gating库(管理后台):独立小网页后台(gating_web_admin.py)并入数富管理后台,
* 共用主库同一张 biz_brand_gating 表。
*
* <p>页面权限键 {@code admin_brand_gating}(后台菜单 column_key,经
* {@link PermissionMenuService#requireConsolePageAccess} 把门);鉴权第一道门由
* <p>本模块以「品牌Gating库」页签嵌在「店铺数据重复检查」页(DuplicateCheckPage.vue)里,
* 不再占用独立菜单行(V164 菜单已由 V166 删除)。因此页面权限键跟随宿主页
* {@code admin_shop_data_duplicate_check},经
* {@link PermissionMenuService#requireConsolePageAccess} 把门;鉴权第一道门由
* AdminApiGuardFilter 的 /api/admin 前缀兜底(见 ApiGuardCoverageAuditor 口径)。</p>
*/
@RestController
@@ -43,8 +45,8 @@ import org.springframework.web.bind.annotation.RestController;
@Tag(name = "品牌Gating库(后台)", description = "品牌Gating库的列表/筛选/统计、单条新增、启停、删除与批量导入。")
public class BrandGatingController {
/** 后台菜单权限键(columns.column_key,与 V164 菜单行同键)。 */
private static final String PERMISSION_KEY = "admin_brand_gating";
/** 宿主页「店铺数据重复检查」的后台菜单权限键(columns.column_key)。 */
private static final String PERMISSION_KEY = "admin_shop_data_duplicate_check";
private static final String DENIAL_MESSAGE = "无权访问品牌Gating库";
private final BrandGatingService brandGatingService;
@@ -110,7 +112,7 @@ public class BrandGatingController {
return ApiResponse.success(brandGatingService.importRows(body));
}
/** 逐端点把门:登录态 + 后台菜单权限(admin_brand_gating)。 */
/** 逐端点把门:登录态 + 宿主页后台菜单权限(admin_shop_data_duplicate_check)。 */
private void requireBrandGatingAccess(HttpServletRequest request) {
AdminUserEntity operator = adminAuthSupport.requireUser(request);
permissionMenuService.requireConsolePageAccess(operator, DENIAL_MESSAGE, PERMISSION_KEY);
@@ -57,7 +57,8 @@ public class ClientCredentialController {
vo.setMysqlPassword(blankToEmpty(clientCredentialProperties.getMysqlPassword()));
// 续期与容忍是两件事:容忍让存量客户端不被 7 天边界掐断,这条把新客户端拉回正常轨道
vo.setJwt(jwtService.issue(user.getId(), user.getUsername(),
adminAuthSupport.currentDeviceId(request)));
adminAuthSupport.currentDeviceId(request),
user.getSessionEpoch() == null ? 0L : user.getSessionEpoch()));
// 只记身份不记值:便于事后回答"哪个账号在哪台机器上领过紫鸟口令"
log.info("[client-credentials] 下发凭据 userId={} username={} deviceId={}",
user.getId(), user.getUsername(), adminAuthSupport.currentDeviceId(request));
@@ -22,6 +22,8 @@ public class TaskFileJobEntity {
private String jobType;
private String status;
private Integer retryCount;
/** 卡死扫描的重排次数(V173),与真失败 retry_count 分账;达到上限后扫描侧判死。 */
private Integer stuckRequeueCount;
private String errorMessage;
private String resultFileUrl;
private LocalDateTime createdAt;
@@ -159,6 +159,12 @@ public class TaskDistributedLockService {
return moduleType.trim().toLowerCase(Locale.ROOT).replace('_', '-');
}
/** 同包用例读数:这把锁是否被标成续期丢失(良性停续期必须为 false)。 */
boolean activeRenewalLost(String lockName) {
ActiveTaskLockRegistry.ActiveLock lock = activeLocks.snapshot().get(lockName);
return lock != null && lock.isRenewalLost();
}
/** 运维与同包用例的读数:当前在持的任务锁 → 已持有时长(毫秒)。空表=这台进程没有在持的锁。 */
Map<String, Long> activeHeldMillis() {
long now = System.currentTimeMillis();
@@ -327,15 +333,26 @@ public class TaskDistributedLockService {
/**
* 停止看门狗续期,但**不释放**锁:父任务已终态时,本节点不该再给这把锁续命;
* 仍由 {@link #close()} 那次带 token 的 RELEASE 收尾,避免把接管者的锁误删。
*
* <p>默认按「续期丢失」标记,保留给真正的续期失败路径;父任务终态这种良性停续期
* 必须传 {@code markLost=false},否则 {@code _prod_watch} 的 ACTIVE_LOST 会把良性
* 停续期计成异常。
*/
public void stopRenewal(String reason) {
stopRenewal(reason, true);
}
public void stopRenewal(String reason, boolean markLost) {
if (heldLock.closed.compareAndSet(false, true)) {
cancelRenewal(heldLock);
activeLocks.markRenewalLost(heldLock.lockName);
log.warn("[task-lock] 停止续期 lockName={} heldMs={} reason={}",
if (markLost) {
activeLocks.markRenewalLost(heldLock.lockName);
}
log.warn("[task-lock] 停止续期 lockName={} heldMs={} reason={} markLost={}",
heldLock.lockName,
System.currentTimeMillis() - heldLock.acquiredAtMillis,
reason);
reason,
markLost);
}
}
@@ -28,6 +28,12 @@ public class TaskFileJobService {
public static final String JOB_TYPE_ASSEMBLE_RESULT = "ASSEMBLE_RESULT";
public static final int MAX_RETRY_COUNT = 5;
/**
* 卡死重排的独立上限。B28 之后卡死重排不扣 retry_count,若没有上限,一个无异常挂住的
* job 会被无限放回 PENDING,并让 TaskResumeService 永远跳过克隆重试。20 次是
* 「慢执行」与「真卡死」的分界:按 5 分钟 stuck 阈值约 100 分钟,远大于现网合法慢单。
*/
public static final int MAX_STUCK_REQUEUE = 20;
private final TaskFileJobMapper taskFileJobMapper;
private final ApplicationEventPublisher applicationEventPublisher;
@@ -419,16 +425,41 @@ public class TaskFileJobService {
// RUNNING 行放回队列,retry_count 只由真失败(markFailed)推进。之前这里 +1 并在
// MAX 时就地判死,把"补检一轮要几十分钟"的慢执行误记成一次失败——现网一枚 760 行
// 的外观专利被扫描连扣 4 次,逼到「文件生成任务运行超时,已达到最大重试次数」后
// 0 字节收场。代价(拍板时已接受):每次重跑都无异常挂住的真卡死会循环重排、
// 不再有扫描侧终态判死;判死只剩 markFailed 这一条真失败的路。
// 0 字节收场。卡死次数改用独立列 stuck_requeue_count 记账,达到上限才判死;
// 这样既保住"慢执行不扣预算",又不会让无异常挂住的 job 无限重排。
LocalDateTime now = LocalDateTime.now();
int stuckCount = job.getStuckRequeueCount() == null ? 0 : job.getStuckRequeueCount();
if (stuckCount >= MAX_STUCK_REQUEUE) {
int failed = taskFileJobMapper.update(null, new LambdaUpdateWrapper<TaskFileJobEntity>()
.eq(TaskFileJobEntity::getId, job.getId())
.eq(TaskFileJobEntity::getStatus, "RUNNING")
.eq(job.getUpdatedAt() != null, TaskFileJobEntity::getUpdatedAt, job.getUpdatedAt())
.isNull(job.getUpdatedAt() == null, TaskFileJobEntity::getUpdatedAt)
.set(TaskFileJobEntity::getStatus, "FAILED")
.set(TaskFileJobEntity::getRetryCount, MAX_RETRY_COUNT)
.set(TaskFileJobEntity::getStuckRequeueCount, MAX_STUCK_REQUEUE)
.set(TaskFileJobEntity::getErrorMessage,
"文件生成任务连续 " + MAX_STUCK_REQUEUE + " 次卡死重排,已判失败")
.set(TaskFileJobEntity::getUpdatedAt, now)
.set(TaskFileJobEntity::getFinishedAt, now)
.set(TaskFileJobEntity::getTerminalCallbackAt, null));
if (failed > 0) {
exhaustedJobIds.add(job.getId());
log.warn("[task-file-job][STUCK_REQUEUE_EXHAUSTED] jobId={} taskId={} moduleType={} stuckRequeueCount={}",
job.getId(), job.getTaskId(), job.getModuleType(), stuckCount);
}
continue;
}
int nextStuckCount = stuckCount + 1;
int updated = taskFileJobMapper.update(null, new LambdaUpdateWrapper<TaskFileJobEntity>()
.eq(TaskFileJobEntity::getId, job.getId())
.eq(TaskFileJobEntity::getStatus, "RUNNING")
.eq(job.getUpdatedAt() != null, TaskFileJobEntity::getUpdatedAt, job.getUpdatedAt())
.isNull(job.getUpdatedAt() == null, TaskFileJobEntity::getUpdatedAt)
.set(TaskFileJobEntity::getStatus, "PENDING")
.set(TaskFileJobEntity::getErrorMessage, "文件生成任务运行超时,已重新排队")
.set(TaskFileJobEntity::getStuckRequeueCount, nextStuckCount)
.set(TaskFileJobEntity::getErrorMessage,
"文件生成任务运行超时,已重新排队(第 " + nextStuckCount + " 次)")
.set(TaskFileJobEntity::getUpdatedAt, now)
.set(TaskFileJobEntity::getFinishedAt, null)
.set(TaskFileJobEntity::getTerminalCallbackAt, null));
@@ -24,6 +24,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@Slf4j
@Service
@@ -199,15 +200,26 @@ public class TaskResultFileJobWorker {
private ScheduledFuture<?> startJobHeartbeat(TaskFileJobEntity job) {
long interval = Math.max(1000L, heartbeatIntervalMillis);
AtomicBoolean terminalStopped = new AtomicBoolean(false);
return jobHeartbeatExecutor.scheduleWithFixedDelay(() -> {
if (terminalStopped.get()) {
return;
}
try {
if (taskFileJobService.isOwningTaskTerminal(job.getTaskId())) {
TaskDistributedLockService.LockHandle handle = activeLockHandles.get(job.getId());
if (handle != null) {
handle.stopRenewal("parent-task-terminal");
log.info("[task-file-job] stopped lock renewal because parent task is terminal jobId={} taskId={} moduleType={} stage=TERMINAL_STOP_RENEW",
if (terminalStopped.compareAndSet(false, true)) {
TaskDistributedLockService.LockHandle handle = activeLockHandles.get(job.getId());
if (handle != null) {
// 良性停续期:父任务已终态,不是续期失败,不能打 renewalLost=true 造成假告警
handle.stopRenewal("parent-task-terminal", false);
}
// 终态后不再 touchRunning:否则 job 的 updated_at 一直被刷新,
// job 级 stuck 扫描永远看不到这行,成为不可回收的 RUNNING 遗骸。
taskFileJobService.markFailedPermanent(job, "父任务已终态,结果作业不再执行");
log.info("[task-file-job] parent task terminal; stopped renewal and failed job jobId={} taskId={} moduleType={} stage=TERMINAL_STOP_RENEW",
job.getId(), job.getTaskId(), job.getModuleType());
}
return;
}
taskFileJobService.touchRunning(job.getId());
} catch (Exception ex) {
@@ -15,6 +15,11 @@ public class CostGuardStatusVo {
private LocalDateTime pausedAt;
private TodayUsage today;
private WindowUsage window;
/**
* window 是本实例最近一轮扫描的读数,不是集群全局值。前端据此标注「本节点读数」,
* 避免把单个实例的窗口值误读成全局速率。
*/
private boolean windowLocalOnly;
private Thresholds thresholds;
private List<SharedSecretWarningVo> sharedWarnings;
@@ -0,0 +1,67 @@
package com.nanri.aiimage.modules.usersecret.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.service.NodeSharedStore;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.time.Duration;
/**
* 损耗扫描基线(用户 → 当日累计三元组 + 采样时刻),跨实例共享。
*
* <p>此前基线是单实例内存,而扫描锁是集群级:持锁方一轮换,新持锁方拿到的
* {@code before} 可能是几分钟前的,增量会被当成 60 秒窗口,导致误暂停;反过来新实例
* 首轮只建基线,又会漏掉一次窗口。基线落 Redis 后,任何实例接手都按真实 elapsed 归一化。
*
* <p>用 {@link NodeSharedStore#getFresh} 而不是 {@code get}:本地缓存优先会让每个实例
* 永远读自己的旧基线,正好是这次要修的问题。
*/
@Service
public class CostGuardBaselineStore {
private static final Duration TTL = Duration.ofHours(26);
private final NodeSharedStore<Long, Baseline> store;
@org.springframework.beans.factory.annotation.Autowired
public CostGuardBaselineStore(StringRedisTemplate redis, ObjectMapper objectMapper) {
this.store = new NodeSharedStore<>("cost-guard:baseline", TTL, Baseline.class,
redis, objectMapper, 0L);
}
/** 测试用:只传一个共享 store,两个调度器实例即可模拟跨实例读数。 */
CostGuardBaselineStore(NodeSharedStore<Long, Baseline> store) {
this.store = store;
}
public Baseline get(Long userId) {
return store.getFresh(userId);
}
public void put(Long userId, Baseline baseline) {
store.putNow(userId, baseline);
}
public void remove(Long userId) {
store.remove(userId);
}
/** Jackson 需要无参构造;字段公开以保持序列化形状稳定。 */
public static class Baseline {
public long proxy;
public long llmCalls;
public long llmTokens;
public long capturedAtMillis;
public Baseline() {
}
public Baseline(long proxy, long llmCalls, long llmTokens, long capturedAtMillis) {
this.proxy = proxy;
this.llmCalls = llmCalls;
this.llmTokens = llmTokens;
this.capturedAtMillis = capturedAtMillis;
}
}
}
@@ -28,7 +28,10 @@ import java.util.concurrent.ConcurrentHashMap;
* 每分钟全量 GROUP BY 的成本可以忽略。
*
* <p>只在本轮真正「跨过」阈值时暂停:用户排查后点继续会调 {@link #resetBaseline(Long)},
* 基线重置为当前值,不会因为日累计仍高于阈值而被立刻再次暂停。
* 基线对齐到当前值,不会因为日累计仍高于阈值而被立刻再次暂停。
*
* <p>基线落在 {@link CostGuardBaselineStore}(Redis),不是单实例内存:扫描锁是集群级的,
* 持锁方轮换后新实例必须按真实 elapsed 归一化增量,否则会把多分钟增量当成 60 秒窗口误暂停。
*/
@Service
@RequiredArgsConstructor
@@ -43,10 +46,11 @@ public class CostGuardScheduler {
private final SharedSecretMonitorService sharedSecretMonitorService;
private final NotificationDispatchService notificationDispatchService;
private final DistributedJobLockService distributedJobLockService;
private final CostGuardBaselineStore baselineStore;
private final Map<Long, UserTotals> previousTotals = new ConcurrentHashMap<>();
private final Map<Long, WindowDelta> lastDeltas = new ConcurrentHashMap<>();
private volatile long lastSharedRefreshAt = 0L;
private volatile long lastSharedNotifyAt = 0L;
@Scheduled(fixedDelayString = "${aiimage.cost-guard.scan-interval-ms:60000}",
initialDelayString = "${aiimage.cost-guard.scan-initial-delay-ms:30000}")
@@ -56,6 +60,9 @@ public class CostGuardScheduler {
}
var lock = distributedJobLockService.tryLock(LOCK_NAME, Duration.ofMinutes(2));
if (lock == null) {
// 非持锁方也要刷新共享密钥快照:否则本实例 status() 永远读到空 warningsByUser,
// 用户看到的「没有共用」与真实情况相反。通知只由持锁方发(见 runOnce)。
maybeRefreshShared(false);
log.info("[cost-guard] 另一实例持有损耗扫描锁,跳过本轮");
return;
}
@@ -68,53 +75,70 @@ public class CostGuardScheduler {
/** 一轮扫描;package-private 供单测直接驱动,不依赖调度线程。 */
void runOnce() {
long nowMillis = System.currentTimeMillis();
int windowSeconds = Math.max(60, properties.getWindowSeconds());
Map<Long, UserTotals> current = loadTodayTotals();
Map<Long, WindowDelta> deltas = new LinkedHashMap<>();
int paused = 0;
for (Map.Entry<Long, UserTotals> entry : current.entrySet()) {
Long userId = entry.getKey();
UserTotals now = entry.getValue();
UserTotals before = previousTotals.get(userId);
CostGuardBaselineStore.Baseline before = baselineStore.get(userId);
boolean advanceBaseline = true;
if (before == null) {
// 首轮只建基线:启动瞬间没有「增量」可比,拿日累计当分钟增量会误伤正在跑的大批次
// 首轮:窗口增量不判(启动前的大批次不是分钟增量),但日累计已经越线必须暂停,
// 否则重启/首次部署后当天永远不会被拦。
deltas.put(userId, WindowDelta.ZERO);
continue;
if (!guardService.isPaused(userId)) {
String reason = dailyBreachReason(now);
if (reason != null && !pauseAndNotify(userId, reason)) {
// 写失败不推进基线:下一轮仍按「已越线」重试,否则 crossed(before, now)
// 会因 before 被抬到阈值之上而永远不再触发。
advanceBaseline = false;
} else if (reason != null) {
paused++;
}
}
} else {
WindowDelta raw = new WindowDelta(
Math.max(0, now.proxy - before.proxy),
Math.max(0, now.llmCalls - before.llmCalls),
Math.max(0, now.llmTokens - before.llmTokens));
long elapsedSeconds = Math.max(1L, (nowMillis - before.capturedAtMillis) / 1000L);
WindowDelta windowDelta = normalize(raw, elapsedSeconds, windowSeconds);
deltas.put(userId, windowDelta);
if (!guardService.isPaused(userId)) {
String reason = breachReason(now, before, windowDelta, windowSeconds);
if (reason != null && !pauseAndNotify(userId, reason)) {
advanceBaseline = false;
} else if (reason != null) {
paused++;
}
}
}
WindowDelta delta = new WindowDelta(
Math.max(0, now.proxy - before.proxy),
Math.max(0, now.llmCalls - before.llmCalls),
Math.max(0, now.llmTokens - before.llmTokens));
deltas.put(userId, delta);
if (guardService.isPaused(userId)) {
continue;
if (advanceBaseline) {
baselineStore.put(userId, new CostGuardBaselineStore.Baseline(
now.proxy, now.llmCalls, now.llmTokens, nowMillis));
}
String reason = breachReason(now, before, delta);
if (reason == null) {
continue;
}
guardService.pause(userId, reason);
notificationDispatchService.pushToUser(userId, NotificationService.SCENE_COST_GUARD,
NotificationService.LEVEL_ERROR, "已自动暂停任务:代理/LLM 消耗异常", reason
+ "。请检查「密钥设置」与正在跑的任务配置,排查后点击客户端提示里的「继续任务」。",
null);
paused++;
log.warn("[cost-guard] 触发暂停 userId={} reason={}", userId, reason);
}
previousTotals.clear();
previousTotals.putAll(current);
lastDeltas.clear();
lastDeltas.putAll(deltas);
maybeRefreshShared();
maybeRefreshShared(true);
log.info("[cost-guard] 扫描完成 用户={} 本轮暂停={} 共享密钥快照={}ms",
current.size(), paused, sharedSecretMonitorService.snapshotAtMillis());
}
/** 用户点击继续时调用:把基线对齐到当前值,避免下一轮拿历史累计再次触发。 */
/**
* 用户点击继续时调用:把基线对齐到当前值(而不是删掉基线)。删掉会让下一轮走「首轮」
* 分支;首轮现在会按日累计判暂停,用户刚点继续就会被立刻再暂停。
*/
public void resetBaseline(Long userId) {
if (userId == null) {
return;
}
previousTotals.remove(userId);
UserTotals current = todayTotals(userId);
baselineStore.put(userId, new CostGuardBaselineStore.Baseline(
current.proxy, current.llmCalls, current.llmTokens, System.currentTimeMillis()));
lastDeltas.remove(userId);
}
@@ -158,21 +182,35 @@ public class CostGuardScheduler {
return totals;
}
private String breachReason(UserTotals now, UserTotals before, WindowDelta delta) {
int seconds = Math.max(60, properties.getWindowSeconds());
/** 暂停写成功才发通知、才计数:写失败时谎报「已暂停」比不暂停更糟。 */
private boolean pauseAndNotify(Long userId, String reason) {
if (!guardService.pause(userId, reason)) {
log.warn("[cost-guard] 暂停写入失败,本轮不通知 userId={} reason={}", userId, reason);
return false;
}
notificationDispatchService.pushToUser(userId, NotificationService.SCENE_COST_GUARD,
NotificationService.LEVEL_ERROR, "已自动暂停任务:代理/LLM 消耗异常", reason
+ "。请检查「密钥设置」与正在跑的任务配置,排查后点击客户端提示里的「继续任务」。",
null);
log.warn("[cost-guard] 触发暂停 userId={} reason={}", userId, reason);
return true;
}
private String breachReason(UserTotals now, CostGuardBaselineStore.Baseline before,
WindowDelta delta, int windowSeconds) {
if (properties.getProxyWindowThreshold() > 0
&& delta.proxy >= properties.getProxyWindowThreshold()) {
return "最近约 " + seconds + " 秒代理提取 " + delta.proxy + " 次,超过阈值 "
&& delta.proxy() >= properties.getProxyWindowThreshold()) {
return "最近约 " + windowSeconds + " 秒代理提取 " + delta.proxy() + " 次,超过阈值 "
+ properties.getProxyWindowThreshold() + " 次";
}
if (properties.getLlmWindowCallsThreshold() > 0
&& delta.llmCalls >= properties.getLlmWindowCallsThreshold()) {
return "最近约 " + seconds + " 秒 LLM 调用 " + delta.llmCalls + " 次,超过阈值 "
&& delta.llmCalls() >= properties.getLlmWindowCallsThreshold()) {
return "最近约 " + windowSeconds + " 秒 LLM 调用 " + delta.llmCalls() + " 次,超过阈值 "
+ properties.getLlmWindowCallsThreshold() + " 次";
}
if (properties.getLlmWindowTokensThreshold() > 0
&& delta.llmTokens >= properties.getLlmWindowTokensThreshold()) {
return "最近约 " + seconds + " 秒 LLM 消耗 " + delta.llmTokens + " tokens,超过阈值 "
&& delta.llmTokens() >= properties.getLlmWindowTokensThreshold()) {
return "最近约 " + windowSeconds + " 秒 LLM 消耗 " + delta.llmTokens() + " tokens,超过阈值 "
+ properties.getLlmWindowTokensThreshold();
}
if (crossed(before.proxy, now.proxy, properties.getDailyProxyThreshold())) {
@@ -190,18 +228,73 @@ public class CostGuardScheduler {
return null;
}
/** 首轮专用:没有上一轮基线可比,只按「已经越线」判日累计,不判窗口增量。 */
private String dailyBreachReason(UserTotals now) {
if (properties.getDailyProxyThreshold() > 0
&& now.proxy >= properties.getDailyProxyThreshold()) {
return "今日代理提取累计 " + now.proxy + " 次,超过当日上限 "
+ properties.getDailyProxyThreshold() + " 次";
}
if (properties.getDailyLlmCallsThreshold() > 0
&& now.llmCalls >= properties.getDailyLlmCallsThreshold()) {
return "今日 LLM 调用累计 " + now.llmCalls + " 次,超过当日上限 "
+ properties.getDailyLlmCallsThreshold() + " 次";
}
if (properties.getDailyLlmTokensThreshold() > 0
&& now.llmTokens >= properties.getDailyLlmTokensThreshold()) {
return "今日 LLM 消耗累计 " + now.llmTokens + " tokens,超过当日上限 "
+ properties.getDailyLlmTokensThreshold();
}
return null;
}
private boolean crossed(long before, long now, long threshold) {
return threshold > 0 && before < threshold && now >= threshold;
}
private void maybeRefreshShared() {
/**
* 把「elapsed 秒的真实增量」换算成 60 秒窗口。持锁方轮换、扫描被拖长、进程重启后
* 首轮接手都会让 elapsed > windowSeconds;不归一化就是把多分钟增量当一分钟判。
*
* <p>只向下缩放(elapsed 大于窗口时),elapsed 小于窗口时直接采用原始增量:
* 短间隔的原始增量是保守下界,放大它只会在调度抖动时制造误暂停。
*/
private WindowDelta normalize(WindowDelta raw, long elapsedSeconds, int windowSeconds) {
if (elapsedSeconds <= windowSeconds) {
return raw;
}
return new WindowDelta(
scaleToWindow(raw.proxy(), elapsedSeconds, windowSeconds),
scaleToWindow(raw.llmCalls(), elapsedSeconds, windowSeconds),
scaleToWindow(raw.llmTokens(), elapsedSeconds, windowSeconds));
}
private long scaleToWindow(long value, long elapsedSeconds, int windowSeconds) {
if (value <= 0) {
return 0;
}
// 向上取整:宁可早一秒判红,也不把刚好越线的批次放过
return Math.max(1L, (value * windowSeconds + elapsedSeconds - 1) / elapsedSeconds);
}
/**
* 共享密钥快照按 shared-scan-interval-ms 刷新。通知只由持锁方发,避免三台实例各推一遍;
* 但非持锁方也要刷新快照,否则它的 status() 永远看不到共享提示。
*/
private void maybeRefreshShared(boolean notify) {
long now = System.currentTimeMillis();
if (now - lastSharedRefreshAt < Math.max(60_000L, properties.getSharedScanIntervalMs())) {
long interval = Math.max(60_000L, properties.getSharedScanIntervalMs());
boolean refreshDue = now - lastSharedRefreshAt >= interval;
boolean notifyDue = notify && now - lastSharedNotifyAt >= interval;
if (!refreshDue && !notifyDue) {
return;
}
lastSharedRefreshAt = now;
// 不自己吞:runOnce 里这是最后一步,抛出由 scan() 的 [cost-guard] WARN 记名,下一间隔再试
sharedSecretMonitorService.refresh();
if (notifyDue) {
lastSharedNotifyAt = now;
}
// 不自己吞:refresh 失败由 scan() 的 [cost-guard] WARN 记名,下一间隔再试
sharedSecretMonitorService.refresh(notifyDue);
}
public static final class UserTotals {
@@ -30,6 +30,7 @@ public class CostGuardService {
vo.setToday(new CostGuardStatusVo.TodayUsage(today.proxy, today.llmCalls, today.llmTokens));
vo.setWindow(new CostGuardStatusVo.WindowUsage(windowSeconds,
delta.proxy(), delta.llmCalls(), delta.llmTokens()));
vo.setWindowLocalOnly(true);
vo.setThresholds(new CostGuardStatusVo.Thresholds(
properties.getProxyWindowThreshold(),
properties.getLlmWindowCallsThreshold(),
@@ -49,6 +49,14 @@ public class SharedSecretMonitorService {
/** 扫描全部密钥配置,刷新「谁和谁共用同一把」的快照,并给使用者推当日一次提示。 */
public synchronized void refresh() {
refresh(true);
}
/**
* @param notify 是否推当日提示。多实例下只有持锁方传 true,其余实例只刷新快照,
* 否则每台实例各推一遍(虽然通知有去重键,但仍是三倍写入与扇出)。
*/
public synchronized void refresh(boolean notify) {
if (!properties.isEnabled() || properties.getSharedAccountsThreshold() <= 0) {
warningsByUser = Map.of();
snapshotAtMillis = System.currentTimeMillis();
@@ -89,7 +97,9 @@ public class SharedSecretMonitorService {
type, group.module.label(), accounts, threshold, message);
for (Long userId : group.userIds) {
next.computeIfAbsent(userId, key -> new ArrayList<>()).add(warning);
pushDailyWarning(userId, group.module, entry.getKey(), warning);
if (notify) {
pushDailyWarning(userId, group.module, entry.getKey(), warning);
}
}
}
warningsByUser = next;
@@ -55,19 +55,25 @@ public class UserTaskGuardService {
return state;
}
public void pause(Long userId, String reason) {
/**
* @return 写入是否成功。调用方只有拿到 true 才能发「已暂停」通知;
* 此前该方法吞掉异常,扫描器无论成败都通知,数据库抖动时用户被告知已暂停、任务却仍在派发。
*/
public boolean pause(Long userId, String reason) {
if (userId == null || userId <= 0) {
return;
return false;
}
String safeReason = truncate(reason);
try {
guardMapper.pause(userId, safeReason);
cache.remove(userId);
log.warn("[cost-guard] 已暂停账号新任务 userId={} reason={}", userId, safeReason);
return true;
} catch (Exception ex) {
// 暂停失败不能拖垮扫描线程:下一轮还会再判一次,异常留在日志里
log.error("[cost-guard] 暂停写入失败 userId={} reason={} err={}",
userId, safeReason, ex.getMessage(), ex);
return false;
}
}
@@ -18,6 +18,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Pattern;
/**
@@ -117,13 +118,16 @@ public class WorkbenchClientPackageService {
}
/**
* 签发直传预签名:对象 key 由「毫秒时间戳 + 安全版本号」组成,每次上传互不覆盖。
* 签发直传预签名:对象 key 由「毫秒时间戳 + 随机 UUID + 安全版本号」组成。
* UUID 是 CR-08 的修复点:只靠毫秒时间戳时,多实例时钟相同或同毫秒两名管理员
* 上传同一版本会生成同一个 key,后一次 PUT 覆盖前一次对象、先确认的 DB 行摘要失效。
*/
public Map<String, Object> presignPackage(String fileName, String version) {
String normalizedName = requireZipFileName(fileName);
String normalizedVersion = requireVersion(version);
String objectKey = OBJECT_KEY_PREFIX
+ OBJECT_NAME_FORMATTER.format(LocalDateTime.now())
+ "-" + UUID.randomUUID().toString().replace("-", "")
+ "-" + safeVersionKey(normalizedVersion) + ".zip";
log.info("[workbench-client] 签发客户端包直传预签名 version={} fileName={} objectKey={} expirySeconds={}",
normalizedVersion, normalizedName, objectKey, PRESIGN_EXPIRY_SECONDS);
@@ -154,27 +158,44 @@ public class WorkbenchClientPackageService {
normalizedVersion, objectKey);
throw new BusinessException("版本号与上传对象不匹配,请重新上传");
}
// 幂等:重复 confirm 同一对象返回已登记行,不再报错(CR-08)
WorkbenchClientPackageEntity existing = packageMapper.selectOne(
new LambdaQueryWrapper<WorkbenchClientPackageEntity>()
.eq(WorkbenchClientPackageEntity::getObjectKey, objectKey)
.last("LIMIT 1"));
if (existing != null) {
log.info("[workbench-client] 直传确认幂等命中 id={} objectKey={}", existing.getId(), objectKey);
return toItemMap(existing);
}
long size = ossStorageService.workbenchClientObjectSize(objectKey);
if (size < 0) {
log.warn("[workbench-client] 直传确认失败:对象不存在 objectKey={}", objectKey);
throw new BusinessException("对象存储中未找到上传的安装包,请重新上传");
}
if (size <= 0) {
// 0 字节对象(空文件/中断上传)会被客户端拿到后解压失败,直接拒绝并清残
log.warn("[workbench-client] 直传对象为空,清理残留 objectKey={}", objectKey);
ossStorageService.removeWorkbenchClientObject(objectKey);
throw new BusinessException("上传的安装包为空文件,请重新上传");
}
if (size > MAX_UPLOAD_BYTES) {
log.warn("[workbench-client] 直传对象超过大小上限,清理残留 objectKey={} bytes={}", objectKey, size);
ossStorageService.removeWorkbenchClientObject(objectKey);
throw new BusinessException("文件超过允许的大小限制");
}
Long sameVersion = packageMapper.selectCount(new LambdaQueryWrapper<WorkbenchClientPackageEntity>()
.eq(WorkbenchClientPackageEntity::getVersion, normalizedVersion));
if (sameVersion != null && sameVersion > 0) {
// 同版本不同字节会让已升级的装机按版本号比较后不升级,客户端还会拿到错误摘要
log.warn("[workbench-client] 直传确认失败:版本已存在 version={} objectKey={}",
normalizedVersion, objectKey);
throw new BusinessException("该版本已存在,请提升版本号或先删除旧包");
}
String sha256 = ossStorageService.workbenchClientObjectSha256(objectKey);
if (sha256 == null || sha256.length() != 64) {
log.warn("[workbench-client] 直传确认失败:摘要计算异常 objectKey={} sha256={}", objectKey, sha256);
throw new BusinessException("安装包摘要计算失败,请重新上传");
}
Long duplicated = packageMapper.selectCount(new LambdaQueryWrapper<WorkbenchClientPackageEntity>()
.eq(WorkbenchClientPackageEntity::getObjectKey, objectKey));
if (duplicated != null && duplicated > 0) {
log.warn("[workbench-client] 直传确认失败:对象已被记录,重复提交 objectKey={}", objectKey);
throw new BusinessException("该安装包已登记,请勿重复提交");
}
WorkbenchClientPackageEntity entity = new WorkbenchClientPackageEntity();
entity.setVersion(normalizedVersion);
@@ -0,0 +1,23 @@
-- =============================================================
-- 迁移 V172__user_session_epoch
-- 立账:ops 仓 docs/audit/CODE_REVIEW_REPORT.md CR-01(2026-10-25)
--
-- 1. 变更目的:给 users 增加会话世代。旧 JWT 没有服务端黑名单,登出只靠 users.machine
-- 上的 revoked@ 标记失效;单设备开关关闭时登录会把这个标记清空,旧令牌重新可用。
-- 世代随登录、登出各 +1,令牌签发时写入 epoch claim,鉴权时与库中值比对,
-- 旧令牌在登出后、以及在任何一次新登录后都立即失效。
--
-- 2. 口径:
-- - 列 NOT NULL DEFAULT 0;存量用户为 0,升级前签发的旧令牌没有 epoch claim 时
-- 按 0 读取,因此不会被本次上线统一踢下线;用户下一次登录/登出推进后旧令牌失效。
-- - machine 的互踢逻辑保持不变;单设备开关关闭时不再依赖清空 revoked@ 标记表达新会话。
--
-- 3. 影响表与数据量:users 加一列,存量行默认 0;MySQL 8.0 起 ADD COLUMN 走 INSTANT,
-- 不重建表。本机无生产库连接,未做上线前 dry-run;上线后由 Flyway 自动应用。
--
-- 4. 回滚(如需):ALTER TABLE `users` DROP COLUMN `session_epoch`;
-- 回滚前需同步回滚应用代码,否则新代码读不到列会启动失败(Expand-and-Contract 的反向)。
-- =============================================================
ALTER TABLE `users`
ADD COLUMN `session_epoch` BIGINT NOT NULL DEFAULT 0 COMMENT '会话世代:登录/登出各 +1' AFTER `machine`;
@@ -0,0 +1,24 @@
-- =============================================================
-- 迁移 V173__task_file_job_stuck_requeue_count
-- 立账:ops 仓 docs/audit/CODE_REVIEW_REPORT.md CR-03(2026-10-25)
--
-- 1. 变更目的:给卡死重排单独记账。B28 拍板「RUNNING 卡死重排不消耗 retry_count」
-- 之后,一个无异常挂住的 job 会被无限放回 PENDING,并且 TaskResumeService 会因此
-- 永远跳过克隆重试。本列只统计 stuck 扫描的重排次数,与真失败重试预算分账;
-- 达到上限(20)时扫描侧直接判 FAILED,保留「慢执行不扣预算」的口径。
--
-- 2. 口径:
-- - NOT NULL DEFAULT 0,存量行从 0 起算;
-- - 只由 TaskFileJobService.resetStuckRunningJobsDetailed 递增;
-- - 开机回收(reclaimOwnedRunningJobs)不递增:发版不是作业的错。
--
-- 3. 影响表与数据量:biz_task_file_job 加一列,存量行默认 0;MySQL 8.0 起 ADD COLUMN
-- 走 INSTANT,不重建表。未连生产库 dry-run,随下一次发版由 Flyway 自动应用。
--
-- 4. 回滚(如需):ALTER TABLE `biz_task_file_job` DROP COLUMN `stuck_requeue_count`;
-- 回滚前需同步回滚应用代码。
-- =============================================================
ALTER TABLE `biz_task_file_job`
ADD COLUMN `stuck_requeue_count` INT NOT NULL DEFAULT 0 COMMENT '卡死重排次数(独立于 retry_count)'
AFTER `retry_count`;
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nanri.aiimage.modules.auth.mapper.LoginUserMapper">
<update id="incrementSessionEpoch">
UPDATE users
SET session_epoch = session_epoch + 1
WHERE id = #{userId}
</update>
</mapper>
@@ -101,7 +101,9 @@ class MigratedSqlEquivalenceTest {
"UserSecretUsageMapper#sumByUserAndModule",
"UserSecretUsageMapper#sumForUser",
// B28(2026-10-25)新写的 1 条主键点查:父任务是否已终态。
"TaskFileJobMapper#selectFileTaskStatus");
"TaskFileJobMapper#selectFileTaskStatus",
// CR-01(2026-10-25)新写的 1 条写侧语句:会话世代原子自增。
"LoginUserMapper#incrementSessionEpoch");
/** 账本 5 列:接口 / 方法 / @Select 种类 / 是否含常量插值 / 注解原文(Java 反转义后那条串)。 */
private static final int LEDGER_COLUMNS = 5;
@@ -80,7 +80,9 @@ class SqlLayeringConventionTest {
// 126 → 128:同轮 {@code UserTaskGuardMapper} 的 pause/resume 两条写侧语句从注解迁进 XML。
// 128 → 129:B28 新增 {@code TaskFileJobMapper#selectFileTaskStatus}(按主键读父任务状态),
// 新写语句按约定直接进 XML,同轮登记进 MigratedSqlEquivalenceTest.NOT_MIGRATED(88 → 89)。
private static final int XML_STATEMENT_TOTAL = 129;
// 129 → 130:CR-01 新增 {@code LoginUserMapper#incrementSessionEpoch}(会话世代原子自增),
// 新写语句直接进 mapper/auth/LoginUserMapper.xml,同轮登记进 NOT_MIGRATED。
private static final int XML_STATEMENT_TOTAL = 130;
/**
* wrapper 里夹带函数级 SQL 的现状("这不是简单单表"的证据),文件名 → 条数:
@@ -126,6 +126,70 @@ class ExcelStreamReaderParseFailureTest {
assertEquals("Nike", seen.toString());
}
@Test
void ooxmlCallsSheetTotalAfterHeader() throws Exception {
Path good = writeWorkbook();
java.util.List<String> events = new java.util.ArrayList<>();
ExcelStreamReader.readFirstSheet(good.toFile(), new ExcelStreamReader.SheetRowHandler() {
@Override
public void onHeader(String sheetName, Integer sheetNo, Map<Integer, String> headerMap) {
events.add("header:" + headerMap.get(0));
}
@Override
public void onSheetTotal(String sheetName, Integer sheetNo, Integer approximateTotalRows) {
events.add("total:" + approximateTotalRows);
}
@Override
public void onRow(String sheetName, Integer sheetNo, int rowIndex,
Map<Integer, String> headerMap, Map<Integer, String> rowMap) {
}
});
assertEquals("header:品牌", events.get(0));
assertTrue(events.size() >= 2 && events.get(1).startsWith("total:"),
"onSheetTotal 必须在 onHeader 之后回调,实际=" + events);
}
@Test
void hssfCallsSheetTotalAfterHeader() throws Exception {
Path file = tempDir.resolve("order.xls");
try (HSSFWorkbook workbook = new HSSFWorkbook()) {
Sheet sheet = workbook.createSheet("Sheet1");
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("品牌");
Row data = sheet.createRow(1);
data.createCell(0).setCellValue("Nike");
try (OutputStream out = Files.newOutputStream(file)) {
workbook.write(out);
}
}
java.util.List<String> events = new java.util.ArrayList<>();
ExcelStreamReader.readFirstSheet(file.toFile(), new ExcelStreamReader.SheetRowHandler() {
@Override
public void onHeader(String sheetName, Integer sheetNo, Map<Integer, String> headerMap) {
events.add("header:" + headerMap.get(0));
}
@Override
public void onSheetTotal(String sheetName, Integer sheetNo, Integer approximateTotalRows) {
events.add("total:" + approximateTotalRows);
}
@Override
public void onRow(String sheetName, Integer sheetNo, int rowIndex,
Map<Integer, String> headerMap, Map<Integer, String> rowMap) {
}
});
assertEquals("header:品牌", events.get(0));
assertTrue(events.size() >= 2 && events.get(1).startsWith("total:"),
"xls 路径同样必须先表头、后总行数,实际=" + events);
}
private Path writeWorkbook() throws IOException {
Path file = tempDir.resolve("ok.xlsx");
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
@@ -96,6 +96,20 @@ class AdminAuthSupportTest {
assertThatCode(() -> support.requireUser(requestWithBearer("t"))).doesNotThrowAnyException();
}
@Test
void requireUserRejectsTokenFromBeforeLogoutOrRelogin() {
// 库中世代已是 2,token 还签着 1:登出/新登录之后旧令牌必须 401,与单设备开关无关
AdminUserEntity user = user(7L, "normal", 0, null, null);
user.setSessionEpoch(2L);
AdminAuthSupport support = support(user, "devA", false, 1L);
BusinessException ex = catchThrowableOfType(
() -> support.requireUser(requestWithBearer("t")), BusinessException.class);
assertThat(ex.getCode()).isEqualTo(401);
assertThat(ex.getMessage()).contains("登录已过期");
}
// ---------- 内部令牌:轮换过渡期新旧两把同时接受 ----------
@Test
@@ -199,6 +213,12 @@ class AdminAuthSupportTest {
}
private AdminAuthSupport support(AdminUserEntity user, String claimDeviceId, boolean singleDeviceEnabled) {
return support(user, claimDeviceId, singleDeviceEnabled,
user.getSessionEpoch() == null ? 0L : user.getSessionEpoch());
}
private AdminAuthSupport support(AdminUserEntity user, String claimDeviceId, boolean singleDeviceEnabled,
long claimSessionEpoch) {
JwtService jwtService = mock(JwtService.class);
AdminUserMapper userMapper = mock(AdminUserMapper.class);
AuthProperties props = mock(AuthProperties.class);
@@ -206,6 +226,7 @@ class AdminAuthSupportTest {
Claims claims = mock(Claims.class);
when(claims.getSubject()).thenReturn(String.valueOf(user.getId()));
when(claims.get("deviceId")).thenReturn(claimDeviceId);
when(claims.get("sessionEpoch")).thenReturn(claimSessionEpoch);
when(jwtService.parse("t")).thenReturn(claims);
when(jwtService.parseWithVerdict("t")).thenReturn(
new JwtService.ParseResult(JwtService.Verdict.VALID, claims));
@@ -37,7 +37,7 @@ class JwtVerdictTest {
@Test
void freshTokenIsValidAndParseStillReturnsClaims() {
JwtService s = service(SECRET);
String token = s.issue(7L, "小魏16", "dev-1");
String token = s.issue(7L, "小魏16", "dev-1", 3L);
JwtService.ParseResult result = s.parseWithVerdict(token);
assertThat(result.verdict()).isEqualTo(JwtService.Verdict.VALID);
assertThat(result.isValid()).isTrue();
@@ -59,7 +59,7 @@ class JwtVerdictTest {
@Test
void wrongKeyIsInvalidNotExpired() {
String forged = service("9999999999012345678901234567890123456789")
.issue(7L, "小魏16", "dev-1");
.issue(7L, "小魏16", "dev-1", 3L);
JwtService.ParseResult result = service(SECRET).parseWithVerdict(forged);
assertThat(result.verdict()).isEqualTo(JwtService.Verdict.INVALID);
assertThat(result.claims()).isNull();
@@ -11,6 +11,7 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -91,6 +92,14 @@ class MigrationInventoryTest {
*/
private static final Set<Integer> RETIRED_HOLES = Set.of(132, 133, 148, 168, 169);
/**
* 文件名版本与头注释版本不一致的历史件。V170 的头注释误写成 V168;它已应用到生产,
* 按 AGENTS.md「已发布迁移一个字节都不许改」不能回改(CR-13)。文件名才是 Flyway
* 认的号,这里把它显式登记成唯一豁免;新增任何不一致都会判红。
*/
private static final Map<String, String> KNOWN_HEADER_VERSION_MISMATCH =
Map.of("V170__user_task_guard.sql", "V168");
@Test
void integerVersionsSequentialFromOne() throws IOException {
List<Migration> all = parseAll(versionFiles());
@@ -144,6 +153,24 @@ class MigrationInventoryTest {
@Test
void headerVersionMatchesFileNameExceptKnownImmutableMismatch() throws IOException {
Map<String, String> mismatches = new LinkedHashMap<>();
Pattern fileNameVersion = Pattern.compile("^V(\\d+)");
Pattern headerVersion = Pattern.compile("(?m)^--\\s*迁移\\s+V(\\d+)");
for (Path file : versionFiles()) {
String name = file.getFileName().toString();
Matcher fileMatcher = fileNameVersion.matcher(name);
Matcher headerMatcher = headerVersion.matcher(Files.readString(file));
if (fileMatcher.find() && headerMatcher.find()
&& !fileMatcher.group(1).equals(headerMatcher.group(1))) {
mismatches.put(name, "V" + headerMatcher.group(1));
}
}
assertEquals(KNOWN_HEADER_VERSION_MISMATCH, mismatches,
"迁移头注释的版本号必须与文件名一致;V170 是已应用件,只能登记为豁免,不能回改");
}
@Test
void checksumStableAcrossReads() throws IOException {
Map<String, String> first = sha256All(versionFiles());
@@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
@@ -57,6 +58,7 @@ class AuthServiceCompanyTest {
// 本组用例只关心公司身份,单设备互踢那条路径留给 AuthServiceTest
when(authProperties.isSingleDeviceEnabled()).thenReturn(false);
when(jwtService.ttlSeconds()).thenReturn(604800L);
when(loginUserMapper.incrementSessionEpoch(7L)).thenReturn(1);
service = new AuthService(loginUserMapper, passwordEncoder, jwtService,
mock(PermissionMenuService.class), authProperties, companyService,
mock(AccountExpiryService.class));
@@ -85,7 +87,7 @@ class AuthServiceCompanyTest {
private void stubSuccessfulLogin(LoginUserEntity user) {
when(loginUserMapper.selectOne(any())).thenReturn(user);
when(passwordEncoder.matches("pwd", "hash")).thenReturn(true);
when(jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
}
@Test
@@ -137,7 +139,7 @@ class AuthServiceCompanyTest {
when(claims.get("deviceId")).thenReturn("devA");
when(jwtService.parse("t")).thenReturn(claims);
when(loginUserMapper.selectById(7L)).thenReturn(user);
when(jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
when(companyService.nameOrEmpty(5L)).thenReturn("厦门米椿花");
LoginResultVo vo = service.checkLogin("t", null);
@@ -22,6 +22,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.catchThrowableOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
@@ -48,7 +49,7 @@ class AuthServiceTest {
Fixture f = fixture(true);
when(f.loginUserMapper.selectOne(any())).thenReturn(f.user("devA"));
when(f.passwordEncoder.matches("pwd", "hash")).thenReturn(true);
when(f.jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(f.jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
LoginRequest request = new LoginRequest();
request.setUsername("u7");
@@ -59,7 +60,9 @@ class AuthServiceTest {
// 绑定写入当前设备(last-login-wins),签发的 token 也用当前设备
verify(f.loginUserMapper, times(1)).update(any(), any());
verify(f.jwtService).issue(7L, "u7", "devB");
verify(f.loginUserMapper).incrementSessionEpoch(7L);
// 登录推进世代(0 → 1),新令牌必须签上推进后的值
verify(f.jwtService).issue(7L, "u7", "devB", 1L);
assertThat(vo.getDeviceId()).isEqualTo("devB");
}
@@ -68,7 +71,7 @@ class AuthServiceTest {
Fixture f = fixture(false);
when(f.loginUserMapper.selectOne(any())).thenReturn(f.user("devA"));
when(f.passwordEncoder.matches("pwd", "hash")).thenReturn(true);
when(f.jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(f.jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
LoginRequest request = new LoginRequest();
request.setUsername("u7");
@@ -87,7 +90,7 @@ class AuthServiceTest {
Fixture f = fixture(false);
when(f.loginUserMapper.selectOne(any())).thenReturn(f.user("revoked@7#20261024185618"));
when(f.passwordEncoder.matches("pwd", "hash")).thenReturn(true);
when(f.jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(f.jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
LoginRequest request = new LoginRequest();
request.setUsername("u7");
@@ -115,7 +118,7 @@ class AuthServiceTest {
assertThat(ex.getCode()).isEqualTo(4011);
assertThat(ex.getMessage()).contains("已在其他设备登录");
// 关键:被踢的旧 token 不能在这里换到新 token「复活」
verify(f.jwtService, never()).issue(any(), anyString(), anyString());
verify(f.jwtService, never()).issue(any(), anyString(), anyString(), anyLong());
}
@Test
@@ -124,11 +127,11 @@ class AuthServiceTest {
Claims claims = f.claims("devA");
when(f.jwtService.parse("t")).thenReturn(claims);
when(f.loginUserMapper.selectById(7L)).thenReturn(f.user("devA"));
when(f.jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(f.jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
LoginResultVo vo = f.service.checkLogin("t", null);
verify(f.jwtService).issue(7L, "u7", "devA");
verify(f.jwtService).issue(7L, "u7", "devA", 0L);
assertThat(vo.getDeviceId()).isEqualTo("devA");
}
@@ -155,7 +158,7 @@ class AuthServiceTest {
root.setRole("super_admin");
root.setIsAdmin(1);
when(f.loginUserMapper.selectById(7L)).thenReturn(root);
when(f.jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(f.jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
assertThatCode(() -> f.service.checkLogin("t", null)).doesNotThrowAnyException();
}
@@ -166,11 +169,59 @@ class AuthServiceTest {
Claims claims = f.claims("devB");
when(f.jwtService.parse("t")).thenReturn(claims);
when(f.loginUserMapper.selectById(7L)).thenReturn(f.user("devA"));
when(f.jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(f.jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
assertThatCode(() -> f.service.checkLogin("t", null)).doesNotThrowAnyException();
}
// ---------- 会话世代:登出/新登录必须让旧 token 失效 ----------
@Test
void checkLoginRejectsTokenFromBeforeLogoutOrRelogin() {
Fixture f = fixture(false);
LoginUserEntity user = f.user("");
user.setSessionEpoch(2L);
Claims claims = f.claims("devA", 1L);
when(f.jwtService.parse("old-token")).thenReturn(claims);
when(f.loginUserMapper.selectById(7L)).thenReturn(user);
BusinessException ex = catchThrowableOfType(
() -> f.service.checkLogin("old-token", null), BusinessException.class);
assertThat(ex.getCode()).isEqualTo(401);
assertThat(ex.getMessage()).contains("登录已过期");
verify(f.jwtService, never()).issue(any(), anyString(), anyString(), anyLong());
}
@Test
void logoutAdvancesSessionEpochEvenWhenMachineIsEmpty() {
// 单设备关闭时 machine 往往为空:旧实现按 machine 做 CAS,命中 0 行 = 登出没留下吊销。
Fixture f = fixture(false);
Claims claims = f.claims("devA", 0L);
when(f.jwtService.parse("t")).thenReturn(claims);
when(f.loginUserMapper.selectById(7L)).thenReturn(f.user(""));
f.service.logoutByToken("t");
verify(f.loginUserMapper).incrementSessionEpoch(7L);
}
@Test
void staleTokenCannotLogOutANewerSession() {
// 旧令牌(世代 1)不能把已经登录到世代 2 的新会话一起登出
Fixture f = fixture(false);
LoginUserEntity user = f.user("");
user.setSessionEpoch(2L);
Claims claims = f.claims("devA", 1L);
when(f.jwtService.parse("t")).thenReturn(claims);
when(f.loginUserMapper.selectById(7L)).thenReturn(user);
f.service.logoutByToken("t");
verify(f.loginUserMapper, never()).incrementSessionEpoch(anyLong());
verify(f.loginUserMapper, never()).update(any(), any());
}
// ---------- 夹具 ----------
private Fixture fixture(boolean singleDeviceEnabled) {
@@ -181,6 +232,7 @@ class AuthServiceTest {
AuthProperties authProperties = mock(AuthProperties.class);
when(authProperties.isSingleDeviceEnabled()).thenReturn(singleDeviceEnabled);
when(jwtService.ttlSeconds()).thenReturn(604800L);
when(loginUserMapper.incrementSessionEpoch(7L)).thenReturn(1);
AuthService service = new AuthService(loginUserMapper, passwordEncoder, jwtService,
permissionMenuService, authProperties, mock(CompanyService.class),
mock(AccountExpiryService.class));
@@ -202,9 +254,14 @@ class AuthServiceTest {
}
Claims claims(String deviceId) {
return claims(deviceId, 0L);
}
Claims claims(String deviceId, long sessionEpoch) {
Claims claims = mock(Claims.class);
when(claims.getSubject()).thenReturn("7");
when(claims.get("deviceId")).thenReturn(deviceId);
when(claims.get("sessionEpoch")).thenReturn(sessionEpoch);
return claims;
}
}
@@ -464,12 +464,12 @@ class BrandGatingServiceTest {
}
@Test
@DisplayName("控制器契约:权限键 admin_brand_gating、逐端点 requireUser + requireConsolePageAccess 把门")
@DisplayName("控制器契约:随宿主页用 admin_shop_data_duplicate_check 权限键、逐端点 requireUser + requireConsolePageAccess 把门")
void controllerGatesEveryEndpointWithPermissionKey() throws Exception {
String source = Files.readString(Path.of(
"src/main/java/com/nanri/aiimage/modules/brandgating/controller/BrandGatingController.java"),
StandardCharsets.UTF_8);
assertThat(source).contains("PERMISSION_KEY = \"admin_brand_gating\"");
assertThat(source).contains("PERMISSION_KEY = \"admin_shop_data_duplicate_check\"");
assertThat(source).contains("adminAuthSupport.requireUser(request)");
assertThat(source).contains("requireConsolePageAccess(operator, DENIAL_MESSAGE, PERMISSION_KEY)");
// 6 个端点方法都必须先过把门助手
@@ -49,7 +49,7 @@ class ClientCredentialRenewalTest {
when(adminAuthSupport.requireUser(any())).thenReturn(user);
when(adminAuthSupport.currentInternalToken()).thenReturn("token-x");
when(adminAuthSupport.currentDeviceId(any())).thenReturn("dev-1");
when(jwtService.issue(eq(7L), eq("小魏16"), eq("dev-1"))).thenReturn("jwt-fresh");
when(jwtService.issue(eq(7L), eq("小魏16"), eq("dev-1"), eq(0L))).thenReturn("jwt-fresh");
configureAll();
ApiResponse<ClientCredentialVo> response = newController().credentials(request);
assertThat(response.isSuccess()).isTrue();
@@ -19,6 +19,25 @@ import static org.mockito.Mockito.when;
*/
class TaskDistributedLockServiceStopRenewalTest {
@Test
void benignTerminalStopRenewalDoesNotMarkRenewalLost() throws Exception {
// 父任务终态是良性停续期;标成 renewalLost=true 会让 _prod_watch 的 ACTIVE_LOST 误报
DistributedJobLockService delegate = mock(DistributedJobLockService.class);
DistributedJobLockService.LockHandle inner = mock(DistributedJobLockService.LockHandle.class);
when(inner.renew(any(Duration.class))).thenReturn(true);
when(delegate.tryLock(anyString(), any(Duration.class))).thenReturn(inner);
TaskDistributedLockService service = new TaskDistributedLockService(delegate);
TaskDistributedLockService.LockHandle handle =
service.acquire("appearance-patent", 32220L, Duration.ofSeconds(1), 0);
handle.stopRenewal("parent-task-terminal", false);
assertEquals(false, service.activeRenewalLost("task:appearance-patent:32220"),
"终态停续期不是续期失败");
handle.close();
}
@Test
void stopRenewalStopsTheWatchdogWithoutReleasingTheLock() throws Exception {
AtomicInteger renewCalls = new AtomicInteger();
@@ -83,6 +83,51 @@ class TaskFileJobServiceTest {
assertUpdateLeavesRetryCountIntact(running.getUpdatedAt());
}
@Test
void stuckRequeueAdvancesIndependentCounterWithoutTouchingRetryBudget() {
TaskFileJobEntity running = runningJob(111L, 1, LocalDateTime.now().minusHours(1));
running.setStuckRequeueCount(19);
TaskFileJobEntity pending = runningJob(111L, 1, LocalDateTime.now());
pending.setStatus("PENDING");
when(taskFileJobMapper.selectList(any())).thenReturn(List.of(running));
when(taskFileJobMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(1);
when(taskFileJobMapper.selectBatchIds(any())).thenReturn(List.of(pending));
TaskFileJobService service = new TaskFileJobService(taskFileJobMapper, applicationEventPublisher);
TaskFileJobService.StuckJobResetResult result = service.resetStuckRunningJobsDetailed(30, 20);
assertEquals(1, result.resetCount());
ArgumentCaptor<LambdaUpdateWrapper<TaskFileJobEntity>> update = updateCaptor();
verify(taskFileJobMapper).update(isNull(), update.capture());
assertTrue(update.getValue().getSqlSet().contains("stuck_requeue_count"));
assertFalse(update.getValue().getSqlSet().contains("retry_count"),
"卡死重排仍不得消耗真失败预算");
assertTrue(update.getValue().getParamNameValuePairs().containsValue(20));
}
@Test
void stuckRequeueAtLimitFailsJobAndHandsItToTerminalCallback() {
TaskFileJobEntity running = runningJob(112L, 1, LocalDateTime.now().minusHours(1));
running.setStuckRequeueCount(TaskFileJobService.MAX_STUCK_REQUEUE);
TaskFileJobEntity failed = runningJob(112L, TaskFileJobService.MAX_RETRY_COUNT, LocalDateTime.now());
failed.setStatus("FAILED");
failed.setErrorMessage("文件生成任务连续 20 次卡死重排,已判失败");
when(taskFileJobMapper.selectList(any())).thenReturn(List.of(running));
when(taskFileJobMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(1);
when(taskFileJobMapper.selectBatchIds(any())).thenReturn(List.of(failed));
TaskFileJobService service = new TaskFileJobService(taskFileJobMapper, applicationEventPublisher);
TaskFileJobService.StuckJobResetResult result = service.resetStuckRunningJobsDetailed(30, 20);
assertEquals(0, result.resetCount(), "达到上限后不再重排");
assertEquals(List.of(failed), result.exhaustedJobs());
ArgumentCaptor<LambdaUpdateWrapper<TaskFileJobEntity>> update = updateCaptor();
verify(taskFileJobMapper).update(isNull(), update.capture());
assertTrue(update.getValue().getParamNameValuePairs().containsValue("FAILED"));
assertTrue(update.getValue().getParamNameValuePairs().containsValue(TaskFileJobService.MAX_RETRY_COUNT));
verify(applicationEventPublisher, never()).publishEvent(any());
}
@Test
void pendingFailureFinalizationIsReturnedAgainWithoutRequeueing() {
TaskFileJobEntity pending = runningJob(106L, TaskFileJobService.MAX_RETRY_COUNT, LocalDateTime.now());
@@ -48,9 +48,12 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.after;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
@@ -349,6 +352,34 @@ class TaskResultFileJobWorkerStuckScanTest {
"只在应用就绪后执行: " + List.of(listener.value()));
}
@Test
void terminalParentHeartbeatStopsTouchingAndFailsJob() throws Exception {
// 父任务在 worker 执行中变终态:心跳不得再 touchRunning(否则 stuck 扫描永远看不到),
// 并主动把 job 收口为 FAILED(终态回调由 markFailedPermanent 打标)。
TaskResultFileJobWorker worker = buildWorker();
setLong(worker, "heartbeatIntervalMillis", 10L);
TaskFileJobEntity job = new TaskFileJobEntity();
job.setId(77L);
job.setTaskId(15L);
job.setModuleType("SIMILAR_ASIN");
job.setStatus("RUNNING");
when(taskFileJobService.isOwningTaskTerminal(15L)).thenReturn(true);
java.lang.reflect.Method method = TaskResultFileJobWorker.class
.getDeclaredMethod("startJobHeartbeat", TaskFileJobEntity.class);
method.setAccessible(true);
java.util.concurrent.ScheduledFuture<?> future =
(java.util.concurrent.ScheduledFuture<?>) method.invoke(worker, job);
try {
// heartbeat 间隔下限 1000ms,首轮在 1s 后才跑;给足调度余量
verify(taskFileJobService, timeout(3000))
.markFailedPermanent(eq(job), contains("父任务已终态"));
verify(taskFileJobService, after(150).never()).touchRunning(77L);
} finally {
future.cancel(true);
}
}
private static void setLong(TaskResultFileJobWorker worker, String fieldName, long value) throws Exception {
Field field = TaskResultFileJobWorker.class.getDeclaredField(fieldName);
field.setAccessible(true);
@@ -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"));
@@ -79,6 +79,19 @@ class WorkbenchClientPackageServiceTest {
assertEquals("https://oss.aishufu.top/client/workbench-client/versions/put", result.get("upload_url"));
}
@Test
void presignKeysAreUniqueForTheSameVersion() {
// 同毫秒/同版本两次预签名必须不同:只靠时间戳会在 A/B 实例或并发上传时覆盖对象
when(ossStorageService.presignWorkbenchClientUpload(anyString(), eq(180))).thenReturn("put");
when(ossStorageService.getWorkbenchClientDownloadUrl(anyString())).thenReturn("download");
String first = (String) service().presignPackage("pkg.zip", "1.2.3").get("object_key");
String second = (String) service().presignPackage("pkg.zip", "1.2.3").get("object_key");
assertTrue(!first.equals(second), "同版本两次上传不得生成同一个对象 key");
assertTrue(first.endsWith("-1.2.3.zip") && second.endsWith("-1.2.3.zip"));
}
@Test
void presignRejectsNonZipAndBlankVersion() {
assertThrows(BusinessException.class, () -> service().presignPackage("pkg.exe", "1.0.0"));
@@ -108,6 +121,47 @@ class WorkbenchClientPackageServiceTest {
verify(packageMapper).insert(any(WorkbenchClientPackageEntity.class));
}
@Test
void confirmIsIdempotentForTheSameObjectKey() {
String objectKey = PREFIX + "20261024100000123-uuid-1.2.3.zip";
WorkbenchClientPackageEntity existing = row(99, "1.2.3", objectKey);
when(packageMapper.selectOne(any())).thenReturn(existing);
Map<String, Object> result = service().confirmPackage(
objectKey, "ShuFuAI-Workbench-1.2.3.zip", "1.2.3");
assertEquals(99L, result.get("id"));
verify(ossStorageService, never()).workbenchClientObjectSize(anyString());
verify(ossStorageService, never()).workbenchClientObjectSha256(anyString());
verify(packageMapper, never()).insert(any(WorkbenchClientPackageEntity.class));
}
@Test
void confirmRejectsEmptyObjectAndCleansItUp() {
String objectKey = PREFIX + "20261024100000123-uuid-1.2.3.zip";
when(ossStorageService.workbenchClientObjectSize(objectKey)).thenReturn(0L);
assertThrows(BusinessException.class, () -> service().confirmPackage(
objectKey, "ShuFuAI-Workbench-1.2.3.zip", "1.2.3"));
verify(ossStorageService).removeWorkbenchClientObject(objectKey);
verify(ossStorageService, never()).workbenchClientObjectSha256(anyString());
verify(packageMapper, never()).insert(any(WorkbenchClientPackageEntity.class));
}
@Test
void confirmRejectsDuplicateVersionToPreventSameVersionDifferentBytes() {
String objectKey = PREFIX + "20261024100000123-uuid-1.2.3.zip";
when(ossStorageService.workbenchClientObjectSize(objectKey)).thenReturn(1234L);
when(packageMapper.selectCount(any())).thenReturn(1L);
assertThrows(BusinessException.class, () -> service().confirmPackage(
objectKey, "ShuFuAI-Workbench-1.2.3.zip", "1.2.3"));
verify(ossStorageService, never()).workbenchClientObjectSha256(anyString());
verify(packageMapper, never()).insert(any(WorkbenchClientPackageEntity.class));
}
@Test
void confirmRejectsVersionThatDoesNotMatchObjectKey() {
String objectKey = PREFIX + "20261024100000123-1.2.3.zip";
@@ -23,6 +23,8 @@ export interface CostGuardStatus {
pausedAt: string | null
today: CostGuardUsage
window: CostGuardUsage & { seconds: number }
/** window 是本节点最近一轮扫描的读数,不是集群全局值。 */
windowLocalOnly?: boolean
thresholds: {
proxyWindow: number
llmCallsWindow: number
@@ -14,8 +14,9 @@ export function usageLine(status: CostGuardStatus | null): string {
if (!status) return ''
const today = status.today
const window = status.window
const scope = status.windowLocalOnly ? '(本节点读数)' : ''
return `今日代理提取 ${formatCount(today.proxy)} 次 · LLM ${formatCount(today.llmCalls)} 次 / ${formatCount(today.llmTokens)} tokens;`
+ `最近 ${window.seconds} 秒:代理 ${formatCount(window.proxy)} 次 · LLM ${formatCount(window.llmCalls)} 次 / ${formatCount(window.llmTokens)} tokens`
+ `最近 ${window.seconds} 秒${scope}:代理 ${formatCount(window.proxy)} 次 · LLM ${formatCount(window.llmCalls)} 次 / ${formatCount(window.llmTokens)} tokens`
}
/** 同一个暂停事件只弹一次:pausedAt 变化(新的自动暂停)才重新弹。 */