feat(credential): 凭据不可自愈 ⇒ 行失败 + 整单终态 + 用户端弹窗(服务端五面)
用户口径(2026-11-12,34061 取证后):没余额要退成失败、直接中断任务、并弹窗提示; 所有涉及代理 IP / LLM 密钥的特性都一样。范围=服务端全部特性,客户端下批。 - 判据唯一出处 common/retry/CredentialFatalClassifier(致命:LLM 欠费/密钥失效、 代理未配置/套餐到期/余额不足/密钥无效;非致命:channel_busy、无空闲通道、冷却、 超时、5xx、限流、出口被拒、技术性、上游不可用);落终态唯一出口 modules/task/service/CredentialFatalTaskCloser(重读现状态、拒碰 TERMINAL、可重入)。 外观专利与相似ASIN 原先逐字重复的 terminalizeOnFatalLlmFailure 改为委托,不留第二份。 - 五面接线:外观专利/相似ASIN、跟价 updateTaskStatusFromLatestRows、 店铺采集 tryFinalizeTask、采集数据 submitResult。判死后回执一律回读库里状态 (采集数据内存 task 停在 RUNNING 会让采集端继续为死路买出口 IP)。 - 行级诚实:凭据致命行不计入已判,回退路不再拿原始标题伪造「侵权/成功」。 - 用户端:credential-fatal-notice.ts(判据)+ credential-fatal-alert.ts(按 taskId:kind 去重)+ CredentialFatalAlert.vue 挂 App.vue;轻量轮询不带文案 ⇒ 跟价/采集数据在失败 终态补一次重型 batch,店铺 batch 补任务级 taskErrorMessage(只给 FAILED、截断 300 字)。 - 三处真死日志按巡检签名正形写「改判死」(scripts/_prod_watch.py 只认 改判死|接管判死), 否则判死数不进账本。 - TaskFailureCategoryGuardrailTest:写点搬进收口器 ⇒ 登记 CredentialFatalTaskCloser, 下限 47→46、updateById 总量 160→159(搬点,不是新增写法)。 判据:CredentialFatalClassifierTest(16)、CollectDataCredentialFatalTest(3)、 PriceTrackTaskServiceTest(8)、ShopDataCrawlScopeMergeTest(11)、 ShopDataCrawlProgressQueryTest(9)、credential-fatal-notice.test.ts(9)、 credential-fatal-alert.test.ts(6)。后端整量 5,464 例 / 0 失败 / 19 skipped。 未纳入本笔:BrandAppearancePatentTab.vue、BrandSimilarAsinTab.vue、TaskItemCard.vue 与两个 Tab 里的按钮/徽章 CSS —— 另一路会话在飞改动,按并发纪律不卷进来。
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* 弹窗队列的去重与取队首(2026-11-12「凭据不可自愈即中断并弹窗」的下半)。
|
||||
*
|
||||
* 靶子是「一次故障弹五次」与「重试后换了原因却不弹」这两头:同一任务同一档只弹一次,
|
||||
* 但同一任务因**另一档**失败时必须再弹——那是要去改另一处。队列本身零框架依赖,
|
||||
* 所以能在这里直接钉,不必起 vue 运行时。
|
||||
*
|
||||
* 跑法:`cd crawler-plugin/frontend-vue && npm test`
|
||||
*/
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import {
|
||||
__resetCredentialFatalAlertForTest,
|
||||
credentialFatalQueueLength,
|
||||
reportCredentialFatal,
|
||||
reportCredentialFatalAfterFetch,
|
||||
subscribeCredentialFatal,
|
||||
takeCredentialFatal,
|
||||
} from '../src/shared/stores/credential-fatal-alert.ts'
|
||||
|
||||
test('同一任务同一档只入队一次(重复轮询/重进页面都吃掉)', () => {
|
||||
__resetCredentialFatalAlertForTest()
|
||||
assert.equal(reportCredentialFatal(32901, '用户未配置代理'), true)
|
||||
assert.equal(reportCredentialFatal(32901, '用户未配置代理'), false)
|
||||
assert.equal(credentialFatalQueueLength(), 1)
|
||||
})
|
||||
|
||||
test('同一任务换了一档要再弹(要去改的是另一处)', () => {
|
||||
__resetCredentialFatalAlertForTest()
|
||||
assert.equal(reportCredentialFatal(32901, '用户未配置代理'), true)
|
||||
assert.equal(reportCredentialFatal(32901, 'insufficient_user_quota'), true)
|
||||
assert.equal(credentialFatalQueueLength(), 2)
|
||||
assert.equal(takeCredentialFatal()?.notice.kind, 'proxy')
|
||||
assert.equal(takeCredentialFatal()?.notice.kind, 'llm_balance')
|
||||
assert.equal(takeCredentialFatal(), null)
|
||||
})
|
||||
|
||||
test('不致命的文案不入队', () => {
|
||||
__resetCredentialFatalAlertForTest()
|
||||
assert.equal(reportCredentialFatal(1, 'LLM http 502: Bad Gateway'), false)
|
||||
assert.equal(reportCredentialFatal(1, ''), false)
|
||||
assert.equal(reportCredentialFatal(0, '用户未配置代理'), false, '非法 taskId 不入队')
|
||||
assert.equal(credentialFatalQueueLength(), 0)
|
||||
})
|
||||
|
||||
test('订阅方在入队时被唤醒(弹窗组件靠它取队首)', () => {
|
||||
__resetCredentialFatalAlertForTest()
|
||||
let calls = 0
|
||||
const off = subscribeCredentialFatal(() => { calls += 1 })
|
||||
reportCredentialFatal(7, '用户未配置代理')
|
||||
assert.equal(calls, 1)
|
||||
off()
|
||||
reportCredentialFatal(8, '用户未配置代理')
|
||||
assert.equal(calls, 1, '退订后不再唤醒')
|
||||
})
|
||||
|
||||
test('取不到文案就算没发生:拉取失败不弹窗', async () => {
|
||||
__resetCredentialFatalAlertForTest()
|
||||
const failed = await reportCredentialFatalAfterFetch(11, async () => { throw new Error('network') })
|
||||
assert.equal(failed, false)
|
||||
assert.equal(credentialFatalQueueLength(), 0)
|
||||
|
||||
const got = await reportCredentialFatalAfterFetch(12, async () => '代理套餐已过期')
|
||||
assert.equal(got, true)
|
||||
assert.equal(takeCredentialFatal()?.taskId, 12)
|
||||
})
|
||||
|
||||
test('已经报过的任务不再为同一档白发一次请求', async () => {
|
||||
__resetCredentialFatalAlertForTest()
|
||||
await reportCredentialFatalAfterFetch(13, async () => '用户未配置代理')
|
||||
let fetched = 0
|
||||
const again = await reportCredentialFatalAfterFetch(13, async () => {
|
||||
fetched += 1
|
||||
return '用户未配置代理'
|
||||
})
|
||||
assert.equal(again, false)
|
||||
assert.equal(fetched, 0, '已有记录就不该再打一次重型请求')
|
||||
assert.equal(credentialFatalQueueLength(), 1)
|
||||
})
|
||||
@@ -0,0 +1,105 @@
|
||||
/**
|
||||
* 「凭据不可自愈 ⇒ 任务已中断」弹窗判据(2026-11-12 用户口径:
|
||||
* 「没余额要退成失败,且直接中断任务,并弹窗提示」)。
|
||||
*
|
||||
* 靶子是**两头的错法**都安静:
|
||||
* ① 判窄了 → 欠费时任务已判死但界面一句话不说,用户以为还在跑;
|
||||
* ② 判宽了 → 青果「没有可用的空闲通道」这类约 3 分钟自愈的占用被弹成「去充值」,
|
||||
* 把用户引去等一个不会来的释放,也把抖动当成故障报。
|
||||
* 所以每个「该弹」的档位都配一条「不许弹」的反向判据。
|
||||
*
|
||||
* 跑法:`cd crawler-plugin/frontend-vue && npm test`
|
||||
*/
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
import {
|
||||
credentialFatalNotice,
|
||||
type CredentialFatalKind,
|
||||
} from '../src/shared/utils/credential-fatal-notice.ts'
|
||||
|
||||
function kindOf(message: string | null | undefined): CredentialFatalKind | null {
|
||||
return credentialFatalNotice(message)?.kind ?? null
|
||||
}
|
||||
|
||||
test('上游 LLM 欠费原文弹「额度不足」', () => {
|
||||
const producer = '{"error":{"message":"预扣费额度失败, 用户[151246]剩余额度: '
|
||||
+ '฿0.029168, 需要预扣费额度: ฿0.032402","type":"new_api_error","code":"insufficient_user_quota"}}'
|
||||
assert.equal(kindOf(producer), 'llm_balance')
|
||||
})
|
||||
|
||||
test('后端已判死并写下的指引句按「哪一句」分档', () => {
|
||||
// 这两句是生产者写进 errorMessage 的(原文 + 指引),前端只认它,不重拼
|
||||
const llmTerminal = 'LLM 网关返回 403;任务已终止:LLM 账户余额不足或密钥失效,不是表格问题;请充值或更换密钥后重新发起任务。'
|
||||
const proxyTerminal = '青果提取失败 NO_AVAILABLE_CHANNEL;任务已终止:代理 IP 套餐已到期或余额不足、或提取链接不可用;请充值、续期或更换提取链接后重新发起任务。'
|
||||
assert.equal(kindOf(llmTerminal), 'llm_balance')
|
||||
assert.equal(kindOf(proxyTerminal), 'proxy')
|
||||
})
|
||||
|
||||
test('代理侧三类不可自愈都弹「代理 IP 不可用」', () => {
|
||||
assert.equal(kindOf('用户未配置代理'), 'proxy')
|
||||
assert.equal(kindOf('代理套餐已过期'), 'proxy')
|
||||
assert.equal(kindOf('代理余额不足'), 'proxy')
|
||||
assert.equal(kindOf('青果提取失败 API_AUTH_DENY'), 'proxy')
|
||||
// 光有「余额不足」分不清是哪一家的余额:兜底按 LLM 额度(后端同一档),弹窗仍会弹
|
||||
assert.equal(kindOf('余额不足'), 'llm_balance')
|
||||
})
|
||||
|
||||
test('LLM 密钥失效与额度不足分两档(要改的地方不同)', () => {
|
||||
assert.equal(kindOf('LLM http 401: invalid api key'), 'key_invalid')
|
||||
assert.equal(kindOf('密钥已失效,请更新'), 'key_invalid')
|
||||
})
|
||||
|
||||
test('可自愈的供应商状态一律不弹', () => {
|
||||
const selfHealing = [
|
||||
// 这一条正是判宽了就会误伤的现网文案:它含「套餐到期」字样,但说的是通道被占
|
||||
'青果回「没有可用的空闲通道」:可能是同账号的其他提取正占着(约 3 分钟释放),也可能是这把 key 的套餐到期/无余量',
|
||||
'代理冷却中,请稍后重试',
|
||||
'LLM http 502: Bad Gateway',
|
||||
'LLM http 429: Too Many Requests',
|
||||
'Connection refused',
|
||||
'aiprice 请求超时',
|
||||
'页面无效:风控墙',
|
||||
]
|
||||
for (const message of selfHealing) {
|
||||
assert.equal(kindOf(message), null, `误弹: ${message}`)
|
||||
}
|
||||
})
|
||||
|
||||
test('没有凭据字样的失败不弹(抖动不是凭据问题)', () => {
|
||||
assert.equal(kindOf('LLM 网关无应答'), null)
|
||||
assert.equal(kindOf('扩展注入失败'), null)
|
||||
assert.equal(kindOf(''), null)
|
||||
assert.equal(kindOf(null), null)
|
||||
assert.equal(kindOf(undefined), null)
|
||||
})
|
||||
|
||||
test('文案本身不许把欠费说成抖动,也不许指错地方', () => {
|
||||
const notices = ['用户未配置代理', 'insufficient_user_quota', 'invalid api key']
|
||||
.map((message) => credentialFatalNotice(message))
|
||||
.filter((notice) => notice !== null)
|
||||
assert.equal(notices.length, 3)
|
||||
for (const notice of notices) {
|
||||
assert.equal(notice!.body.includes('稍后重试'), false)
|
||||
assert.equal(notice!.body.includes('表格有问题'), true, '要明确告诉用户不是数据问题')
|
||||
assert.equal(notice!.title.startsWith('任务已中断'), true)
|
||||
}
|
||||
})
|
||||
|
||||
// ---- 接线绊线:判据写好了但没人调 = 一条永远不会出现的弹窗 ----
|
||||
|
||||
test('五个特性的终态分支都接在同一处上报出口上', () => {
|
||||
const sources: Array<[string, string, string]> = [
|
||||
['外观专利+相似ASIN 共用面板', '../src/shared/composables/useLiveTaskBoard.ts', 'reportCredentialFatal('],
|
||||
['跟价', '../src/pages/brand/components/BrandPriceTrackTab.vue', 'reportCredentialFatalAfterFetch('],
|
||||
['采集数据', '../src/pages/brand/components/BrandCollectDataTab.vue', 'reportCredentialFatalAfterFetch('],
|
||||
['店铺采集', '../src/pages/brand/components/BrandShopDataCrawlTab.vue', 'reportCredentialFatal('],
|
||||
['根组件', '../src/App.vue', '<CredentialFatalAlert />'],
|
||||
]
|
||||
for (const [label, relative, needle] of sources) {
|
||||
const text = readFileSync(fileURLToPath(new URL(relative, import.meta.url)), 'utf8')
|
||||
assert.ok(text.includes(needle), `${label} 没有调用 ${needle}`)
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user