问题:维护窗口 record.owner_job_id 指向失败/待恢复的作业 A,但页面把同 scope
最新作业(更新的成功封存 B)当窗口进度,needs_restore 被显示成“导出 · 已完成”,
需要处理的原窗口因此看起来已经结束。
改法:
- maintenanceSummary 只接受“按 owner_job_id 读到的持有作业”,id 不一致一律按
未读取处理;进度/错误/恢复说明只来自该作业,不拿最新作业兜底。
- 新增 fetchLibraryJobDetail 读取既有只读接口 GET /api/v1/library/jobs/{id},
明确区分 loaded / missing(404) / error(非2xx、结构不符、id 串包) / unread,
不把读取失败吞成“不存在”。
- 恢复可用性只由“维护仍持有 + 本窗口作业未收口 + 当前 scope 等于 owner_scope”
决定;running/pending 不并发恢复,已释放/无记录不提供恢复,跨 scope 给切换提示。
- 入口状态明确标注“维护记录,未实时探测”;held 时说明只有维护记录释放才算结束;
held 但持有作业已完成时写“维护记录未对账”,不提供强制成功。
- load 增加请求代次守卫,晚回包不覆盖更新的窗口;作业详情返回的 id 与请求不符
按读取失败处理。
未改后端准入、网关协议或任何维护记录;未部署。
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { renderToStaticMarkup } from 'react-dom/server'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
|
||||
import { MaintenanceCard, maintenanceSummary, wikiContributionLines } from './ReleaseTargets'
|
||||
import { fetchLibraryJobDetail } from './librarySnapshots'
|
||||
import type { LibraryDiff, LibraryJobRecord, LibraryMaintenanceView } from './librarySnapshots'
|
||||
|
||||
const job: LibraryJobRecord = {
|
||||
@@ -43,7 +44,7 @@ const heldInstance: LibraryMaintenanceView = {
|
||||
|
||||
describe('维护级别/进度/恢复错误', () => {
|
||||
it('实例级持有:显示级别、入口状态、进度与恢复错误,并允许按预备份恢复', () => {
|
||||
const summary = maintenanceSummary(heldInstance, job)
|
||||
const summary = maintenanceSummary(heldInstance, { state: 'loaded', job })
|
||||
expect(summary.level).toBe('实例级')
|
||||
expect(summary.held).toBe(true)
|
||||
expect(summary.ingress).toContain('已关闭')
|
||||
@@ -54,9 +55,21 @@ describe('维护级别/进度/恢复错误', () => {
|
||||
expect(summary.canRecover).toBe(true)
|
||||
|
||||
const html = renderToStaticMarkup(
|
||||
<MaintenanceCard maintenance={heldInstance} job={job} canWrite onRecover={() => undefined} />
|
||||
<MaintenanceCard
|
||||
maintenance={heldInstance}
|
||||
ownerJob={{ state: 'loaded', job }}
|
||||
canWrite
|
||||
onRecover={() => undefined}
|
||||
/>
|
||||
)
|
||||
for (const text of ['目标维护', '实例级', '入口 已关闭', '按预备份恢复', '写入生产 chunks 失败']) {
|
||||
for (const text of [
|
||||
'目标维护',
|
||||
'实例级',
|
||||
'入口(维护记录,未实时探测)',
|
||||
'已关闭',
|
||||
'按预备份恢复',
|
||||
'写入生产 chunks 失败',
|
||||
]) {
|
||||
expect(html).toContain(text)
|
||||
}
|
||||
})
|
||||
@@ -67,7 +80,7 @@ describe('维护级别/进度/恢复错误', () => {
|
||||
expect(summary.held).toBe(false)
|
||||
expect(summary.canRecover).toBe(false)
|
||||
const html = renderToStaticMarkup(
|
||||
<MaintenanceCard maintenance={null} job={undefined} canWrite onRecover={() => undefined} />
|
||||
<MaintenanceCard maintenance={null} ownerJob={{ state: 'unread' }} canWrite onRecover={() => undefined} />
|
||||
)
|
||||
expect(html).toContain('暂无维护窗口')
|
||||
expect(html).not.toContain('按预备份恢复')
|
||||
@@ -85,13 +98,18 @@ describe('维护级别/进度/恢复错误', () => {
|
||||
ingress_state: 'open',
|
||||
},
|
||||
}
|
||||
const summary = maintenanceSummary(released, job)
|
||||
const summary = maintenanceSummary(released, { state: 'loaded', job })
|
||||
expect(summary.held).toBe(false)
|
||||
expect(summary.recoveryError).toBe('')
|
||||
expect(summary.releasedFailure).toContain('写入生产 chunks 失败')
|
||||
|
||||
const html = renderToStaticMarkup(
|
||||
<MaintenanceCard maintenance={released} job={job} canWrite onRecover={() => undefined} />
|
||||
<MaintenanceCard
|
||||
maintenance={released}
|
||||
ownerJob={{ state: 'loaded', job }}
|
||||
canWrite
|
||||
onRecover={() => undefined}
|
||||
/>
|
||||
)
|
||||
expect(html).not.toContain('目标保持维护')
|
||||
expect(html).not.toContain('收尾没有完成')
|
||||
@@ -101,7 +119,12 @@ describe('维护级别/进度/恢复错误', () => {
|
||||
|
||||
it('仍在持有的失败作业保留 held 诊断文案', () => {
|
||||
const html = renderToStaticMarkup(
|
||||
<MaintenanceCard maintenance={heldInstance} job={job} canWrite onRecover={() => undefined} />
|
||||
<MaintenanceCard
|
||||
maintenance={heldInstance}
|
||||
ownerJob={{ state: 'loaded', job }}
|
||||
canWrite
|
||||
onRecover={() => undefined}
|
||||
/>
|
||||
)
|
||||
expect(html).toContain('目标保持维护')
|
||||
expect(html).toContain('收尾没有完成')
|
||||
@@ -116,13 +139,242 @@ describe('维护级别/进度/恢复错误', () => {
|
||||
state: 'recovered',
|
||||
},
|
||||
}
|
||||
const summary = maintenanceSummary(released, job)
|
||||
const summary = maintenanceSummary(released, { state: 'loaded', job })
|
||||
expect(summary.held).toBe(false)
|
||||
expect(summary.canRecover).toBe(false)
|
||||
expect(summary.releasedAt).not.toBe('')
|
||||
})
|
||||
})
|
||||
|
||||
// ---- 维护窗口的作业归属(A 窗口 ≠ 该 scope 最新作业 B)----
|
||||
//
|
||||
// 真实事故(2026-11-03 22:48 CST):对内维护窗口 owner_job_id=libverify-…A 仍是
|
||||
// needs_restore/ingress closed,但 scope 上最新作业是成功封存 B(libseal-…,completed/export)。
|
||||
// 页面把 B 的“导出 · 已完成”当成窗口进度,于是需要处理的窗口看起来已经完成。
|
||||
const ownerVerify: LibraryJobRecord = {
|
||||
id: 'libverify-internal-A',
|
||||
scope: 'internal',
|
||||
kind: 'verify',
|
||||
state: 'failed',
|
||||
stage: 'verify',
|
||||
request_digest: 'v',
|
||||
queue_job_id: 'qv',
|
||||
snapshot_id: '',
|
||||
target_snapshot_id: '',
|
||||
actor: 'admin',
|
||||
error: '恢复验证失败:canary 校验不一致',
|
||||
recovery: '预备份保留;按归档人工核验',
|
||||
created_at: '2026-11-03T09:20:00Z',
|
||||
updated_at: '2026-11-03T09:24:00Z',
|
||||
finished_at: '2026-11-03T09:24:00Z',
|
||||
stages: [],
|
||||
}
|
||||
|
||||
const latestSeal: LibraryJobRecord = {
|
||||
...ownerVerify,
|
||||
id: 'libseal-lib-internal-B',
|
||||
kind: 'seal',
|
||||
state: 'succeeded',
|
||||
stage: 'export',
|
||||
error: '',
|
||||
recovery: '',
|
||||
finished_at: '2026-11-03T09:23:52Z',
|
||||
}
|
||||
|
||||
const unrelatedFailure: LibraryJobRecord = {
|
||||
...latestSeal,
|
||||
id: 'libseal-lib-internal-C',
|
||||
state: 'failed',
|
||||
stage: 'index',
|
||||
error: '封存索引导出失败:与维护窗口无关',
|
||||
finished_at: '2026-11-03T09:25:00Z',
|
||||
}
|
||||
|
||||
const needsRestore: LibraryMaintenanceView = {
|
||||
held: true,
|
||||
instance: 'kgmaint-weknora',
|
||||
instance_maintenance: {
|
||||
instance: 'kgmaint-weknora',
|
||||
owner_scope: 'internal',
|
||||
scope: 'internal',
|
||||
owner_job_id: 'libverify-internal-A',
|
||||
level: 'instance',
|
||||
services: ['app'],
|
||||
ingress_state: 'closed',
|
||||
state: 'needs_restore',
|
||||
acquired_at: '2026-11-03T09:20:00Z',
|
||||
updated_at: '2026-11-03T09:24:00Z',
|
||||
released_at: null,
|
||||
},
|
||||
}
|
||||
|
||||
describe('维护窗口作业归属', () => {
|
||||
it('持有作业 A 失败时显示 A 的进度/错误,不显示较新封存 B 的“导出 · 已完成”', () => {
|
||||
const summary = maintenanceSummary(needsRestore, { state: 'loaded', job: ownerVerify }, 'internal')
|
||||
expect(summary.progress).toContain('校验')
|
||||
expect(summary.progress).toContain('失败')
|
||||
expect(summary.progress).not.toContain('导出')
|
||||
expect(summary.recoveryError).toContain('canary 校验不一致')
|
||||
expect(summary.canRecover).toBe(true)
|
||||
|
||||
const html = renderToStaticMarkup(
|
||||
<MaintenanceCard
|
||||
maintenance={needsRestore}
|
||||
ownerJob={{ state: 'loaded', job: ownerVerify }}
|
||||
scope="internal"
|
||||
canWrite
|
||||
onRecover={() => undefined}
|
||||
/>
|
||||
)
|
||||
expect(html).not.toContain('导出 · 已完成')
|
||||
expect(html).toContain('canary 校验不一致')
|
||||
expect(html).toContain('按预备份恢复')
|
||||
// 入口只是维护记录里的字段,页面不得声称实时探测过服务。
|
||||
expect(html).toContain('维护记录,未实时探测')
|
||||
})
|
||||
|
||||
it('调用方误把较新成功封存 B 传进来时,身份不匹配 ⇒ 明确“持有作业状态未读取”,不冒成功', () => {
|
||||
const summary = maintenanceSummary(needsRestore, { state: 'loaded', job: latestSeal }, 'internal')
|
||||
expect(summary.progress).not.toContain('导出')
|
||||
expect(summary.progress).not.toContain('已完成')
|
||||
expect(summary.progress).toContain('未读取')
|
||||
expect(summary.recoveryError).toBe('')
|
||||
const html = renderToStaticMarkup(
|
||||
<MaintenanceCard
|
||||
maintenance={needsRestore}
|
||||
ownerJob={{ state: 'loaded', job: latestSeal }}
|
||||
scope="internal"
|
||||
canWrite
|
||||
onRecover={() => undefined}
|
||||
/>
|
||||
)
|
||||
expect(html).not.toContain('导出 · 已完成')
|
||||
// 窗口本身仍是 needs_restore:需要处理的原窗口不能被隐藏。
|
||||
expect(html).toContain('needs_restore')
|
||||
})
|
||||
|
||||
it('无关的最新失败作业不把错误串进本窗口', () => {
|
||||
const summary = maintenanceSummary(needsRestore, { state: 'loaded', job: unrelatedFailure }, 'internal')
|
||||
expect(summary.recoveryError).toBe('')
|
||||
expect(summary.progress).not.toContain('封存索引导出失败')
|
||||
})
|
||||
|
||||
it('持有作业仍在运行 ⇒ 同窗口不允许并发恢复', () => {
|
||||
const running: LibraryJobRecord = { ...ownerVerify, state: 'running', stage: 'database', finished_at: null }
|
||||
const summary = maintenanceSummary(needsRestore, { state: 'loaded', job: running }, 'internal')
|
||||
expect(summary.progress).toContain('执行中')
|
||||
expect(summary.canRecover).toBe(false)
|
||||
const html = renderToStaticMarkup(
|
||||
<MaintenanceCard
|
||||
maintenance={needsRestore}
|
||||
ownerJob={{ state: 'loaded', job: running }}
|
||||
scope="internal"
|
||||
canWrite
|
||||
onRecover={() => undefined}
|
||||
/>
|
||||
)
|
||||
expect(html).not.toContain('按预备份恢复')
|
||||
})
|
||||
|
||||
it('窗口已释放的持有作业失败只留历史记录,不提供恢复', () => {
|
||||
const released: LibraryMaintenanceView = {
|
||||
held: false,
|
||||
instance_maintenance: {
|
||||
...needsRestore.instance_maintenance!,
|
||||
state: 'released',
|
||||
ingress_state: 'open',
|
||||
released_at: '2026-11-03T10:00:00Z',
|
||||
},
|
||||
}
|
||||
const summary = maintenanceSummary(released, { state: 'loaded', job: ownerVerify }, 'internal')
|
||||
expect(summary.held).toBe(false)
|
||||
expect(summary.canRecover).toBe(false)
|
||||
expect(summary.releasedFailure).toContain('canary 校验不一致')
|
||||
expect(summary.releasedAt).not.toBe('')
|
||||
})
|
||||
|
||||
it('跨 scope 查看实例级窗口:不给本 scope 投递恢复', () => {
|
||||
const summary = maintenanceSummary(needsRestore, { state: 'loaded', job: ownerVerify }, 'external')
|
||||
expect(summary.canRecover).toBe(false)
|
||||
expect(summary.scopeNote).toContain('internal')
|
||||
expect(summary.scopeNote).toContain('external')
|
||||
const html = renderToStaticMarkup(
|
||||
<MaintenanceCard
|
||||
maintenance={needsRestore}
|
||||
ownerJob={{ state: 'loaded', job: ownerVerify }}
|
||||
scope="external"
|
||||
canWrite
|
||||
onRecover={() => undefined}
|
||||
/>
|
||||
)
|
||||
expect(html).not.toContain('按预备份恢复')
|
||||
})
|
||||
|
||||
it('持有作业详情读取失败/不存在时明确未知,不退回最新作业也不编造成功', () => {
|
||||
const failedRead = maintenanceSummary(needsRestore, { state: 'error', reason: 'HTTP 503' }, 'internal')
|
||||
expect(failedRead.progress).toContain('读取失败')
|
||||
expect(failedRead.progress).toContain('HTTP 503')
|
||||
expect(failedRead.recoveryError).toBe('')
|
||||
|
||||
const missing = maintenanceSummary(needsRestore, { state: 'missing' }, 'internal')
|
||||
expect(missing.progress).toContain('不存在')
|
||||
expect(missing.recoveryError).toBe('')
|
||||
|
||||
const unread = maintenanceSummary(needsRestore, { state: 'unread' }, 'internal')
|
||||
expect(unread.progress).toContain('未读取')
|
||||
// needs_restore 是已确认的维护契约:作业详情读不到也保留恢复入口,由后端按窗口校验。
|
||||
expect(unread.canRecover).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('fetchLibraryJobDetail(只读作业详情)', () => {
|
||||
const originalFetch = globalThis.fetch
|
||||
afterEach(() => {
|
||||
globalThis.fetch = originalFetch
|
||||
})
|
||||
|
||||
it('按 owner id 读取并校验结构', async () => {
|
||||
let seen = ''
|
||||
globalThis.fetch = (async (url: string) => {
|
||||
seen = String(url)
|
||||
return new Response(JSON.stringify({ available: true, job: ownerVerify }), { status: 200 })
|
||||
}) as unknown as typeof fetch
|
||||
const value = await fetchLibraryJobDetail('libverify-internal-A')
|
||||
expect(seen).toBe('/api/v1/library/jobs/libverify-internal-A')
|
||||
expect(value.state).toBe('loaded')
|
||||
expect(value.state === 'loaded' && value.job.id).toBe('libverify-internal-A')
|
||||
})
|
||||
|
||||
it('id 里的特殊字符必须编码,且返回别的作业按读取失败处理', async () => {
|
||||
let seen = ''
|
||||
globalThis.fetch = (async (url: string) => {
|
||||
seen = String(url)
|
||||
return new Response(JSON.stringify({ available: true, job: ownerVerify }), { status: 200 })
|
||||
}) as unknown as typeof fetch
|
||||
const value = await fetchLibraryJobDetail('libverify/internal A')
|
||||
expect(seen).toBe('/api/v1/library/jobs/libverify%2Finternal%20A')
|
||||
// 串包/晚回包:返回的不是请求的那个作业,不能拿去展示。
|
||||
expect(value.state).toBe('error')
|
||||
})
|
||||
|
||||
it('404 是“作业不存在”,不是读取失败', async () => {
|
||||
globalThis.fetch = (async () =>
|
||||
new Response(JSON.stringify({ available: false }), { status: 404 })) as unknown as typeof fetch
|
||||
expect((await fetchLibraryJobDetail('x')).state).toBe('missing')
|
||||
})
|
||||
|
||||
it('5xx 与结构不符是“读取失败”,不能吞成不存在', async () => {
|
||||
globalThis.fetch = (async () => new Response('{}', { status: 503 })) as unknown as typeof fetch
|
||||
const error = await fetchLibraryJobDetail('x')
|
||||
expect(error.state).toBe('error')
|
||||
expect(error.state === 'error' && error.reason).toContain('503')
|
||||
|
||||
globalThis.fetch = (async () =>
|
||||
new Response(JSON.stringify({ available: true, job: { id: 'x' } }), { status: 200 })) as unknown as typeof fetch
|
||||
expect((await fetchLibraryJobDetail('x')).state).toBe('error')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Wiki 贡献级差异展示', () => {
|
||||
const diff: LibraryDiff = {
|
||||
state: 'computed',
|
||||
|
||||
Reference in New Issue
Block a user