fix(monitor): 修复取件死信无法重排与列表 NULL 标志导致的 500

- platform/postgres: listTestPublicationsSQL 的 can_start/can_retry 在 LEFT JOIN
  内容对象为空时算出 NULL,Go 侧 bool 扫描失败使整个列表接口 500,前端三个 lane
  全空(表现为“待入库 0 / 全部任务 0”)。改为 COALESCE(..., false)。
- 新增 POST /api/v1/job-failures/{id}/requeue:取件任务的幂等键绑在版本上,
  失败那次已占住它,重新标注同一文件不会再入队;现在按新轮次 (…:r<n>) 重排、
  复位意图取件状态并把死信标记为 requeued(幂等信封 + 审计字段)。
- web: 死信区对 artifact.fetch 显示「重新入队」;系统设置新增「知识库绑定」分区,
  挂上一直没被渲染的 RoutingBindingsSection,导入目标库可在页面上切换。
- 测试:真实 PostgreSQL 集成测试(NULL 折叠 + 重排轮次/幂等/拒绝)、前端死信按钮
  与设置分区渲染测试。
This commit is contained in:
hermes
2026-10-08 12:09:34 +08:00
parent 22a6bc5004
commit cddc4c90d1
12 changed files with 495 additions and 8 deletions
+11
View File
@@ -55,6 +55,17 @@ describe('SystemSettingsView',()=>{
expect(html).not.toContain('添加内容源')
expect(html).not.toContain('管理员编辑')
})
it('lets an administrator bind the internal/external import knowledge bases from 系统设置',()=>{
// 组件一直在(RoutingBindingsSection),但从未挂到任何页面 —— 导入目标库只能靠
// 部署 env 钉死,换库必须重建容器。
const view=(section:'sources'|'bindings')=>renderToStaticMarkup(<AuthContext.Provider value={authValue('admin')}><SystemSettingsView sources={sources} connections={connections} monitor={monitor} settings={settings} productionEnabled={false} routingCapabilities={{internal_enabled:true,external_enabled:true}} loading={false} saving={false} error={null} saveMessage={null} onSave={vi.fn()} onSourcesChanged={vi.fn()} onRefresh={vi.fn()} initialSection={section} /></AuthContext.Provider>)
expect(view('sources')).toContain('知识库绑定')
const bound=view('bindings')
expect(bound).toContain('id="settings-routing-bindings"')
expect(bound).toContain('按这里的绑定分发到对应 WeKnora 知识库')
// 未读到绑定数据时(SSR 不跑 effect)面板也要如实存在,而不是空白。
expect(bound).toContain('读取中…')
})
it('does not show healthy simulation when the settings data failed to load',()=>{
const html=renderToStaticMarkup(<SystemSettingsView sources={[]} connections={[]} monitor={null} settings={null} productionEnabled={false} routingCapabilities={{internal_enabled:false,external_enabled:false}} loading={false} saving={false} error="加载失败" saveMessage={null} onSave={vi.fn()} onSourcesChanged={vi.fn()} onRefresh={vi.fn()} />)
expect(html).toContain('加载失败');expect(html).not.toContain('模拟运行中')
+4 -1
View File
@@ -2,6 +2,7 @@ import { useCallback, useEffect, useState } from 'react'
import { MembersSection } from './MembersSection'
import { DingtalkSessionControl } from './DingtalkSessionControl'
import { RoutingBindingsSection } from './RoutingBindingsSection'
import { DingtalkSpaceRegistrar } from './DingtalkSpaceRegistrar'
import { FilenameFilterEditor } from './FilenameFilterEditor'
import { useOptionalAuth } from './auth'
@@ -34,11 +35,12 @@ interface Props {
initialSection?: SectionKey
}
type SectionKey = 'sources' | 'services' | 'storage' | 'refresh' | 'members'
type SectionKey = 'sources' | 'services' | 'bindings' | 'storage' | 'refresh' | 'members'
const sections: { key: SectionKey; label: string; adminOnly?: boolean }[] = [
{ key: 'sources', label: '数据源配置' },
{ key: 'services', label: '服务配置' },
{ key: 'bindings', label: '知识库绑定' },
{ key: 'storage', label: '存储与备份' },
{ key: 'refresh', label: '刷新策略' },
{ key: 'members', label: '成员与角色', adminOnly: true },
@@ -258,6 +260,7 @@ export function SystemSettingsView({ sources, connections, monitor, settings, pr
{serviceSettingsValid ? null : <p className="error-message" role="alert">格式不合法:地址需 http:// 或 https:// 开头的纯地址,不能带用户名密码、? 查询串或 # 片段;生产 ID 需为十进制数字;知识库 ID 不能含空格、/、?、# 或 \。</p>}</>
: <small>服务配置仅管理员可修改;当前值:测试地址 {settings.test_weknora_base_url || '未配置'} · 生产地址 {settings.production_weknora_base_url || '未配置'} · 生产 ID {settings.production_tenant_id || '未配置'} · 知识库 ID {settings.production_knowledge_base_id || '未配置'}。</small>}
</section> : null}
{section === 'bindings' ? <RoutingBindingsSection /> : null}
{section === 'storage' ? <section className="settings-panel wide storage-settings-panel" id="settings-storage">
<div className="settings-section-heading"><div><h2>存储与备份</h2><p>文件资源与生产发布前快照使用独立受限配置。</p></div></div>
<div className="storage-settings-grid"><dl><dt>Endpoint / Bucket</dt><dd>尚未配置</dd><dt>AccessKey / Secret</dt><dd>仅保存凭据引用,不可回读</dd><dt>CDN / 公开访问域名</dt><dd>尚未配置</dd><dt>临时签名有效期</dt><dd>待配置</dd></dl><dl><dt>测试备份前缀</dt><dd>/backup/weknora/test/</dd><dt>生产备份前缀</dt><dd>/backup/weknora/prod/</dd><dt>快照保留</dt><dd>最近 30 个生产快照</dd><dt>恢复验证</dt><dd>由生产门禁读回</dd></dl><ul className="target-list"><li>PostgreSQL <b>未配置</b></li><li>Elasticsearch <b>未配置</b></li><li>对象存储 <b>未配置</b></li><li>Neo4j <b>未配置</b></li><li>生产发布请求 <b>{productionEnabled ? '等待管理员核对' : '关闭'}</b></li><li>Release Worker <b>网页不可启用</b></li></ul></div>
+19
View File
@@ -70,4 +70,23 @@ describe('WeKnoraMonitorView',()=>{
expect(html).toContain('已显示 3 / 共 115')
expect(html).toContain('加载更多')
})
it('offers 重新入队 for a 源头取件 dead letter that carries no publication id',()=>{
// 取件死信没有 publication_id,所以不能走发布重试链路;没有这个按钮,一个终态
// 取件失败(例如钉钉会话过期)在界面上就永远救不回来。
const failures=[{id:'jf-fetch',job_id:'job-fetch',job_type:'artifact.fetch',error_code:'artifact_invalid',attempt:1,max_attempts_at_failure:8,state:'open' as const,created_at:'2026-10-08T03:20:35Z',doc_name:'关闭PC2远程桌面弹窗.docx',last_error:'artifact_fetch_rejected: http_401'}]
const html=renderToStaticMarkup(<WeKnoraMonitorView {...baseProps} lane='attention' failures={failures} onRequeueFailure={vi.fn()} />)
expect(html).toContain('源头取件')
expect(html).toContain('关闭PC2远程桌面弹窗.docx')
expect(html).toContain('重新入队')
const busy=renderToStaticMarkup(<WeKnoraMonitorView {...baseProps} lane='attention' failures={failures} requeueingIDs={['jf-fetch']} />)
expect(busy).toContain('重排中…')
expect(busy).not.toContain('>重新入队<')
})
it('does not offer 重新入队 for a 上传解析 dead letter without a publication id',()=>{
const failures=[{id:'jf-upload',job_id:'job-upload',job_type:'test.upload',error_code:'handler_error',attempt:8,max_attempts_at_failure:8,state:'open' as const,created_at:'2026-10-08T03:20:35Z',last_error:'remote_parse_failed'}]
const html=renderToStaticMarkup(<WeKnoraMonitorView {...baseProps} lane='attention' failures={failures} />)
expect(html).toContain('上传解析')
// 段落提示语里始终有“重新入队=再给它一次机会”,所以断言按钮本身不存在。
expect(html).not.toContain('>重新入队</button>')
})
})
+25 -3
View File
@@ -3,7 +3,7 @@ import { useContext, useEffect, useMemo, useState } from 'react'
import { fetchMonitorFiles, fetchMonitorOverview, fetchRunMetrics } from './monitor'
import type { RunMetrics } from './monitor'
import type { KnowledgeProgress, MonitorFilePage, MonitorOverview, MonitorStatus } from './monitor'
import { fetchJobFailures, fetchTestPublications, retryPublication, retryPublicationsBatch, cancelPublicationsBatch, startTestPublications } from './testPublications'
import { fetchJobFailures, fetchTestPublications, retryPublication, retryPublicationsBatch, cancelPublicationsBatch, requeueJobFailure, startTestPublications } from './testPublications'
import type { JobFailure, ProcessingState, TestPublication, TestPublicationPage } from './testPublications'
import { fetchActiveUISettings, settingsRefreshDelays } from './uiSettings'
import { WorkspaceContextTabsSetter } from './workspaceContext'
@@ -87,6 +87,7 @@ interface ViewProps {
loading: boolean
error: string | null
retryingIDs?: string[]
requeueingIDs?: string[]
cancellingIDs?: string[]
starting?: boolean
startMessage?: string
@@ -102,6 +103,7 @@ interface ViewProps {
onRetry?: (id: string) => void
onBatchRetry?: (ids: string[]) => void
onBatchCancel?: (ids: string[]) => void
onRequeueFailure?: (id: string) => void
onStart?: (ids: string[]) => void
onLoadMore?: () => void
onRefresh: () => void
@@ -122,6 +124,7 @@ export function WeKnoraMonitorView({
loading,
error,
retryingIDs = [],
requeueingIDs = [],
cancellingIDs = [],
starting = false,
loadingMore = false,
@@ -136,6 +139,7 @@ export function WeKnoraMonitorView({
onRetry = () => undefined,
onBatchRetry = () => undefined,
onBatchCancel = () => undefined,
onRequeueFailure = () => undefined,
onStart = () => undefined,
onLoadMore = () => undefined,
onRefresh,
@@ -209,7 +213,7 @@ export function WeKnoraMonitorView({
{batchMessage ? <p className="queue-batch-message" role="status">{batchMessage}</p> : null}
{attentionItems.length ? attentionItems.map(actionTask) : emptyLane('没有失败或停滞的任务。')}
<div className="section-head sub"><h3>死信(重试次数用完,系统不再自动处理)</h3><span className="tiny">“源头取件”=从钉钉/AList 把原始文件拉回本地;“上传解析”=把文件送进知识库做解析。死信只是失败任务的登记表,不会改动原始记录;重新入队=再给它一次机会</span></div>
{failures.length ? <ul className="dlq-list">{failures.map((failure) => <li key={failure.id}><span className="monitor-status failed">{failure.error_code}</span><b>{failure.job_type === 'test.upload' ? '上传解析' : failure.job_type === 'artifact.fetch' ? '源头取件' : failure.job_type}</b>{failure.doc_name ? <b>「{failure.doc_name}」</b> : failure.publication_id ? <code>{failure.publication_id}</code> : null}<small>{failure.attempt}/{failure.max_attempts_at_failure} 次 · {new Date(failure.created_at).toLocaleString('zh-CN', { hour12: false })}</small><small>{failureHint(failure)}</small>{failure.publication_id ? <button disabled={retryingIDs.includes(failure.publication_id)} onClick={() => onBatchRetry([failure.publication_id as string])}>重新入队</button> : null}</li>)}</ul> : emptyLane('当前没有等待处理的死信记录。')}
{failures.length ? <ul className="dlq-list">{failures.map((failure) => <li key={failure.id}><span className="monitor-status failed">{failure.error_code}</span><b>{failure.job_type === 'test.upload' ? '上传解析' : failure.job_type === 'artifact.fetch' ? '源头取件' : failure.job_type}</b>{failure.doc_name ? <b>「{failure.doc_name}」</b> : failure.publication_id ? <code>{failure.publication_id}</code> : null}<small>{failure.attempt}/{failure.max_attempts_at_failure} 次 · {new Date(failure.created_at).toLocaleString('zh-CN', { hour12: false })}</small><small>{failureHint(failure)}</small>{failure.job_type === 'artifact.fetch' ? <button disabled={requeueingIDs.includes(failure.id)} onClick={() => onRequeueFailure(failure.id)}>{requeueingIDs.includes(failure.id) ? '重排中…' : '重新入队'}</button> : failure.publication_id ? <button disabled={retryingIDs.includes(failure.publication_id)} onClick={() => onBatchRetry([failure.publication_id as string])}>重新入队</button> : null}</li>)}</ul> : emptyLane('当前没有等待处理的死信记录。')}
</section> : null}
{lane === 'removal' ? <section className="inbox-section" aria-label="需下架">
<div className="section-head"><h2>需下架({laneCounts.removal})</h2><span className="tiny">来源目录里被改为“禁止导入”或撤销标注、但仍在知识库中可检索的文件</span></div>
@@ -265,6 +269,7 @@ export function WeKnoraMonitor() {
const [documentRefreshMs, setDocumentRefreshMs] = useState(15000)
const [resourceRefreshMs, setResourceRefreshMs] = useState(30000)
const [retryingIDs, setRetryingIDs] = useState<string[]>([])
const [requeueingIDs, setRequeueingIDs] = useState<string[]>([])
const [cancellingIDs, setCancellingIDs] = useState<string[]>([])
const [starting, setStarting] = useState(false)
const [startMessage, setStartMessage] = useState('')
@@ -344,6 +349,23 @@ export function WeKnoraMonitor() {
return () => controller.abort()
}, [documentRevision, resourceRevision])
const requeueFailure = async (failureID: string) => {
setRequeueingIDs((current) => [...current, failureID])
setBatchMessage('')
try {
const result = await requeueJobFailure(failureID, '源头取件死信重新入队')
setError(null)
setBatchMessage(result.already_queued
? `该取件任务已在队列中(第 ${result.round} 轮)`
: `已重新入队:${result.doc_name || result.version_id.slice(0, 12)}(第 ${result.round} 轮取件),取回后自动进入待入库`)
setDocumentRevision((value) => value + 1)
} catch (cause) {
setError(cause instanceof Error ? cause.message : '重新取件失败')
} finally {
setRequeueingIDs((current) => current.filter((value) => value !== failureID))
}
}
const retryOne = async (id: string) => {
setRetryingIDs((current) => [...current, id])
try {
@@ -419,5 +441,5 @@ export function WeKnoraMonitor() {
setStarting(false)
}
}
return <WeKnoraMonitorView overview={overview} files={files} queue={queue} plans={plans} selectedKB={selected} status={status} queueStatus={queueStatus} queueScope={queueScope} loading={loading} error={error} retryingIDs={retryingIDs} cancellingIDs={cancellingIDs} starting={starting} startMessage={startMessage} batchMessage={batchMessage} failures={failures} metrics={metrics} onSelectKB={(kb) => { setSelected(kb); setStatus(''); setFiles(null) }} onStatus={(value) => { setStatus(value); setFiles(null) }} onQueueStatus={setQueueStatus} onQueueScope={setQueueScope} lane={lane} onLaneChange={(value) => setLane(value as typeof lane)} onRetry={(id) => void retryOne(id)} onBatchRetry={(ids) => void retryBatch(ids)} onBatchCancel={(ids) => void cancelBatch(ids)} onStart={(ids) => void startPlans(ids)} attention={attention ?? undefined} loadingMore={loadingMore} onLoadMore={() => void loadMoreQueue()} onRefresh={() => { setDocumentRevision((value) => value + 1); setResourceRevision((value) => value + 1) }} />
return <WeKnoraMonitorView overview={overview} files={files} queue={queue} plans={plans} selectedKB={selected} status={status} queueStatus={queueStatus} queueScope={queueScope} loading={loading} error={error} retryingIDs={retryingIDs} requeueingIDs={requeueingIDs} cancellingIDs={cancellingIDs} starting={starting} startMessage={startMessage} batchMessage={batchMessage} failures={failures} metrics={metrics} onSelectKB={(kb) => { setSelected(kb); setStatus(''); setFiles(null) }} onStatus={(value) => { setStatus(value); setFiles(null) }} onQueueStatus={setQueueStatus} onQueueScope={setQueueScope} lane={lane} onLaneChange={(value) => setLane(value as typeof lane)} onRetry={(id) => void retryOne(id)} onBatchRetry={(ids) => void retryBatch(ids)} onBatchCancel={(ids) => void cancelBatch(ids)} onRequeueFailure={(id) => void requeueFailure(id)} onStart={(ids) => void startPlans(ids)} attention={attention ?? undefined} loadingMore={loadingMore} onLoadMore={() => void loadMoreQueue()} onRefresh={() => { setDocumentRevision((value) => value + 1); setResourceRevision((value) => value + 1) }} />
}
+27
View File
@@ -208,3 +208,30 @@ export async function fetchJobFailures(signal?: AbortSignal): Promise<JobFailure
if (!isRecord(value) || !isRecord(value.data) || !Array.isArray(value.data.items)) throw new Error('失败队列响应无效')
return value.data.items as JobFailure[]
}
export interface RequeuedArtifactFetch { failure_id: string; job_id: string; version_id: string; round: number; doc_name?: string; already_queued: boolean }
// 源头取件死信重排:取件任务的幂等键绑在版本上,失败那次已占住它,所以重新标注
// 同一文件不会再入队;这里让后端按新轮次重新排队(新键),并复位意图状态。
export async function requeueJobFailure(failureId: string, reason: string): Promise<RequeuedArtifactFetch> {
const response = await fetch(`/api/v1/job-failures/${encodeURIComponent(failureId)}/requeue`, {
method: 'POST',
headers: { Accept: 'application/json', 'Content-Type': 'application/json', 'Idempotency-Key': newOperationID() },
body: JSON.stringify({ reason }),
})
if (!response.ok) {
const body = (await response.json().catch(() => null)) as { error?: string } | null
throw new Error(body?.error ?? `重新取件失败(HTTP ${response.status})`)
}
const value = await response.json() as unknown
if (!isRecord(value) || !isRecord(value.data)) throw new Error('重新取件响应无效')
const data = value.data as Record<string, unknown>
return {
failure_id: typeof data.failure_id === 'string' ? data.failure_id : '',
job_id: typeof data.job_id === 'string' ? data.job_id : '',
version_id: typeof data.version_id === 'string' ? data.version_id : '',
round: isCount(data.round) ? data.round : 0,
doc_name: typeof data.doc_name === 'string' ? data.doc_name : undefined,
already_queued: data.already_queued === true,
}
}