dcdc8d1ddd
全栈知识治理平台首次入库(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 干净
142 lines
4.7 KiB
Go
142 lines
4.7 KiB
Go
package v071
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"unicode/utf8"
|
|
|
|
"knowledge-governance/internal/platform/weknora"
|
|
releaseworkflow "knowledge-governance/internal/workflows/release"
|
|
)
|
|
|
|
type WeKnoraVerificationClient interface {
|
|
GetKnowledge(ctx context.Context, knowledgeID string) (weknora.Knowledge, error)
|
|
ListChunks(ctx context.Context, knowledgeID string, page, pageSize int, chunkTypes []string) ([]weknora.Chunk, int64, error)
|
|
SearchKnowledge(ctx context.Context, request weknora.SearchRequest) ([]weknora.SearchResult, error)
|
|
}
|
|
|
|
type WeKnoraVerifier struct {
|
|
client WeKnoraVerificationClient
|
|
}
|
|
|
|
func NewWeKnoraVerifier(client WeKnoraVerificationClient) (*WeKnoraVerifier, error) {
|
|
if client == nil {
|
|
return nil, errors.New("WeKnora verification client is required")
|
|
}
|
|
return &WeKnoraVerifier{client: client}, nil
|
|
}
|
|
|
|
func (verifier *WeKnoraVerifier) Verify(ctx context.Context, item releaseworkflow.PlanItem, bundle *DocumentBundle) error {
|
|
if bundle == nil || validateDocumentBundle(bundle) != nil || bundle.Manifest.Contract.KnowledgeID != item.KnowledgeID || bundle.Manifest.Contract.KnowledgeBaseID != item.TestKnowledgeBaseID {
|
|
return errors.New("invalid WeKnora verification bundle")
|
|
}
|
|
knowledge, err := verifier.client.GetKnowledge(ctx, item.KnowledgeID)
|
|
if err != nil || knowledge.ID != item.KnowledgeID || knowledge.KnowledgeBaseID != item.TestKnowledgeBaseID || knowledge.ParseStatus != "completed" {
|
|
return errors.New("production WeKnora knowledge verification failed")
|
|
}
|
|
_, chunkTotal, err := verifier.client.ListChunks(ctx, item.KnowledgeID, 1, 1, bundleChunkTypes(bundle))
|
|
expectedChunks := bundle.Postgres.Table("chunks").RowCount
|
|
if err != nil || expectedChunks < 1 || chunkTotal != int64(expectedChunks) {
|
|
return errors.New("production WeKnora chunk verification failed")
|
|
}
|
|
query := bundleProbeQuery(bundle)
|
|
if query == "" {
|
|
return errors.New("production WeKnora retrieval probe is unavailable")
|
|
}
|
|
results, err := verifier.client.SearchKnowledge(ctx, weknora.SearchRequest{Query: query, KnowledgeIDs: []string{item.KnowledgeID}})
|
|
if err != nil {
|
|
return errors.New("production WeKnora retrieval verification failed")
|
|
}
|
|
for _, result := range results {
|
|
if result.KnowledgeID == item.KnowledgeID {
|
|
return nil
|
|
}
|
|
}
|
|
return errors.New("production WeKnora retrieval verification failed")
|
|
}
|
|
|
|
func (verifier *WeKnoraVerifier) VerifyDisabled(ctx context.Context, item releaseworkflow.PlanItem) error {
|
|
if !validSearchIdentifier(item.KnowledgeID) {
|
|
return errors.New("invalid disabled WeKnora verification plan")
|
|
}
|
|
results, err := verifier.client.SearchKnowledge(ctx, weknora.SearchRequest{
|
|
Query: "governance retrieval disable verification sentinel",
|
|
KnowledgeIDs: []string{item.KnowledgeID},
|
|
})
|
|
if err != nil {
|
|
return errors.New("disabled WeKnora retrieval verification failed")
|
|
}
|
|
for _, result := range results {
|
|
if result.KnowledgeID == item.KnowledgeID {
|
|
return errors.New("disabled WeKnora knowledge remains retrievable")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// bundleChunkTypes lists the distinct chunk_type values sealed in the bundle.
|
|
// WeKnora's chunk list endpoint defaults to text-only results, so verification
|
|
// must ask for exactly the types the released document contains.
|
|
func bundleChunkTypes(bundle *DocumentBundle) []string {
|
|
seen := map[string]bool{}
|
|
types := make([]string, 0, 3)
|
|
for _, raw := range bundle.Postgres.Table("chunks").Rows {
|
|
var row struct {
|
|
ChunkType string `json:"chunk_type"`
|
|
}
|
|
if json.Unmarshal(raw, &row) != nil {
|
|
continue
|
|
}
|
|
chunkType := strings.TrimSpace(row.ChunkType)
|
|
if chunkType == "" || seen[chunkType] || !validSearchIdentifier(chunkType) {
|
|
continue
|
|
}
|
|
seen[chunkType] = true
|
|
types = append(types, chunkType)
|
|
}
|
|
return types
|
|
}
|
|
|
|
func bundleProbeQuery(bundle *DocumentBundle) string {
|
|
for _, document := range bundle.Search.Documents {
|
|
var value map[string]any
|
|
if json.Unmarshal(document.Source, &value) != nil {
|
|
continue
|
|
}
|
|
for _, key := range []string{"content", "question", "answer", "title"} {
|
|
if text, ok := value[key].(string); ok {
|
|
if query := boundedProbeText(text); query != "" {
|
|
return query
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for _, row := range bundle.Postgres.Table("knowledges").Rows {
|
|
var value struct {
|
|
Title string `json:"title"`
|
|
}
|
|
if json.Unmarshal(row, &value) == nil {
|
|
if query := boundedProbeText(value.Title); query != "" {
|
|
return query
|
|
}
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func boundedProbeText(value string) string {
|
|
value = strings.Join(strings.Fields(value), " ")
|
|
if value == "" {
|
|
return ""
|
|
}
|
|
if utf8.RuneCountInString(value) <= 128 {
|
|
return value
|
|
}
|
|
runes := []rune(value)
|
|
return string(runes[:128])
|
|
}
|
|
|
|
var _ ProductionVerifier = (*WeKnoraVerifier)(nil)
|