feat: 知识治理平台(含已验证发布/回滚契约)

全栈知识治理平台首次入库(P1-P10 + 发布链路模拟验证闭环):

- 治理核心:源目录/变更集/人工审核/发布批次/发布执行(fail-closed 闸口)
- 测试通道:WeKnora 测试 KB 上传/轮询/验收;poll 完成时写入
  parser_generation/index_generation,修复 manual/seed 路径发布闸口永久拦截
- 发布适配器 v071(本地 source=测试角色 / 云端 target=生产角色 模拟验证通过):
  * ES 导出/导入 mapping 自适应(.keyword 子字段、可选 tenant_id)
  * OSS locator 支持嵌套 scheme;chunks.seq_id 由目标序列分配
  * bundle 归档同身份重试原子替换;verifier 按 bundle chunk_type 计数
  * 回滚 job payload 支持 prior_execution_id(strict 解码)
- 三通道发布+回滚演练实测:publish 4/4 verified,检索命中,
  rollback 4/4 rolled_back,目标端 PG is_enabled=false、ES is_enabled=false
- 前端 A·Data-console 风格全站统一,120 用例通过
- 门禁:go vet / go test 全包通过,gofmt 干净
This commit is contained in:
2026-09-13 12:02:58 +08:00
committed by 姚章浩
parent ed48c4ceda
commit dcdc8d1ddd
410 changed files with 56661 additions and 1 deletions
+181
View File
@@ -0,0 +1,181 @@
import { newOperationID } from './operationKey'
export type IngestState = 'pending' | 'uploading' | 'uploaded' | 'failed'
export type ProcessingState = 'pending' | 'processing' | 'finalizing' | 'completed' | 'failed' | 'stalled'
export interface TestPublication {
id: string
title: string
ingest_state: IngestState
processing_state: ProcessingState
retrieval_state: string
target_scope: 'internal' | 'external'
change_set_id: string
change_type: 'added' | 'updated'
can_start: boolean
retry_count: number
last_error: string
can_retry: boolean
updated_at: string
}
export interface TestPublicationPage {
items: TestPublication[]
total: number
limit: number
offset: number
}
export interface AcceptanceRecord {
check_type: string
state: string
policy_version: string
evidence_summary: unknown
observed_at: string
}
export interface TestPublicationQuery {
processing_state: ProcessingState | ''
target_scope?: 'internal' | 'external' | ''
limit: number
offset: number
}
export interface StartTestPublicationsResult {
selected: number
started: number
already_started: number
unavailable: number
}
const isRecord = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null
const isCount = (value: unknown): value is number => typeof value === 'number' && Number.isInteger(value) && value >= 0
const ingestStates = new Set<IngestState>(['pending', 'uploading', 'uploaded', 'failed'])
const processingStates = new Set<ProcessingState>(['pending', 'processing', 'finalizing', 'completed', 'failed', 'stalled'])
function parseTestPublication(value: unknown): TestPublication {
if (!isRecord(value) || typeof value.id !== 'string' || typeof value.title !== 'string' ||
typeof value.ingest_state !== 'string' || !ingestStates.has(value.ingest_state as IngestState) ||
typeof value.processing_state !== 'string' || !processingStates.has(value.processing_state as ProcessingState) ||
typeof value.retrieval_state !== 'string' || (value.target_scope !== 'internal' && value.target_scope !== 'external') ||
typeof value.change_set_id !== 'string' ||
(value.change_type !== 'added' && value.change_type !== 'updated') || typeof value.can_start !== 'boolean' ||
!isCount(value.retry_count) || typeof value.last_error !== 'string' || typeof value.can_retry !== 'boolean' || typeof value.updated_at !== 'string') {
throw new Error('invalid test publication response')
}
return {
id: value.id,
title: value.title,
ingest_state: value.ingest_state as IngestState,
processing_state: value.processing_state as ProcessingState,
retrieval_state: value.retrieval_state,
target_scope: value.target_scope,
change_set_id: value.change_set_id,
change_type: value.change_type,
can_start: value.can_start,
retry_count: value.retry_count,
last_error: value.last_error,
can_retry: value.can_retry,
updated_at: value.updated_at,
}
}
export function parseTestPublicationsResponse(value: unknown): TestPublicationPage {
if (!isRecord(value) || !Array.isArray(value.data) || !isCount(value.total) || !isCount(value.limit) || !isCount(value.offset)) {
throw new Error('invalid test publications response')
}
return {
items: value.data.map(parseTestPublication),
total: value.total,
limit: value.limit,
offset: value.offset,
}
}
export async function fetchTestPublications(query: TestPublicationQuery, signal?: AbortSignal): Promise<TestPublicationPage> {
const parameters = new URLSearchParams()
if (query.processing_state) parameters.set('processing_state', query.processing_state)
if (query.target_scope) parameters.set('target_scope', query.target_scope)
parameters.set('limit', String(query.limit))
parameters.set('offset', String(query.offset))
const response = await fetch(`/api/v1/test-publications?${parameters.toString()}`, { headers: { Accept: 'application/json' }, signal })
if (!response.ok) throw new Error(`加载测试发布失败(HTTP ${response.status})`)
return parseTestPublicationsResponse(await response.json())
}
export async function fetchAcceptanceResults(publicationId: string, signal?: AbortSignal): Promise<AcceptanceRecord[]> {
const response = await fetch(`/api/v1/test-publications/${encodeURIComponent(publicationId)}/acceptance`, { headers: { Accept: 'application/json' }, signal })
if (!response.ok) throw new Error(`加载验收结果失败(HTTP ${response.status})`)
const value = await response.json() as unknown
if (!isRecord(value) || !Array.isArray(value.data)) throw new Error('invalid acceptance response')
return value.data as AcceptanceRecord[]
}
export async function retryPublication(publicationId: string): Promise<void> {
const response = await fetch(`/api/v1/test-publications/${encodeURIComponent(publicationId)}/retry`, { method: 'POST', headers: { Accept: 'application/json', 'Idempotency-Key': newOperationID() } })
if (!response.ok) {
const body = (await response.json().catch(() => null)) as { error?: string } | null
throw new Error(body?.error ?? `重试失败(HTTP ${response.status})`)
}
}
export async function startTestPublications(publicationIds: string[], operatorRef: string, reason: string): Promise<StartTestPublicationsResult> {
const response = await fetch('/api/v1/test-publications/start', {
method: 'POST',
headers: { Accept: 'application/json', 'Content-Type': 'application/json', 'Idempotency-Key': newOperationID() },
body: JSON.stringify({ publication_ids: publicationIds, operator_ref: operatorRef, 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) || !isCount(value.data.selected) || !isCount(value.data.started) || !isCount(value.data.already_started) || !isCount(value.data.unavailable)) {
throw new Error('invalid processing start response')
}
return {
selected: value.data.selected,
started: value.data.started,
already_started: value.data.already_started,
unavailable: value.data.unavailable,
}
}
export interface RetryOutcome { publication_id: string; result: 'started' | 'already_running' | 'not_retryable'; detail?: string }
export interface RetryPublicationsBatchResult { batch_key: string; total: number; started: number; already_running: number; not_retryable: number; outcomes: RetryOutcome[] }
export async function retryPublicationsBatch(publicationIds: string[], operatorRef: string, reason: string): Promise<RetryPublicationsBatchResult> {
const response = await fetch('/api/v1/test-publications/retry-batch', {
method: 'POST',
headers: { Accept: 'application/json', 'Content-Type': 'application/json', 'Idempotency-Key': newOperationID() },
body: JSON.stringify({ publication_ids: publicationIds, operator_ref: operatorRef, 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) || !Array.isArray(value.data.outcomes)) throw new Error('批量重试响应无效')
const data = value.data as Record<string, unknown>
const outcomes = Array.isArray(data.outcomes) ? data.outcomes : []
return {
batch_key: typeof data.batch_key === 'string' ? data.batch_key : '',
total: isCount(data.total) ? data.total : 0,
started: isCount(data.started) ? data.started : 0,
already_running: isCount(data.already_running) ? data.already_running : 0,
not_retryable: isCount(data.not_retryable) ? data.not_retryable : 0,
outcomes: outcomes.filter(isRecord).map((item) => ({
publication_id: typeof item.publication_id === 'string' ? item.publication_id : '',
result: item.result === 'started' || item.result === 'already_running' || item.result === 'not_retryable' ? item.result : 'not_retryable',
detail: typeof item.detail === 'string' ? item.detail : undefined,
})),
}
}
export interface JobFailure { id: string; job_id: string; job_type: string; publication_id?: string; source_item_id?: string; error_code: string; attempt: number; max_attempts_at_failure: number; change_set_id?: string; action?: string; target_kb_id?: string; state: 'open' | 'requeued' | 'abandoned'; created_at: string }
export async function fetchJobFailures(signal?: AbortSignal): Promise<JobFailure[]> {
const response = await fetch('/api/v1/job-failures?state=open&limit=50', { headers: { Accept: 'application/json' }, signal })
if (!response.ok) throw new Error(`加载失败队列失败(HTTP ${response.status})`)
const value = await response.json() as unknown
if (!isRecord(value) || !isRecord(value.data) || !Array.isArray(value.data.items)) throw new Error('失败队列响应无效')
return value.data.items as JobFailure[]
}