ea6e56b304
发布通道原来只有三条:PostgreSQL 关系行、Elasticsearch 分片、OSS 对象。 WeKnora 的 wiki 图谱是 wiki_pages 一张纯 PG 表(in_links/out_links 存 slug), 所以它不在任何通道里——撤权后生产 wiki 会一直留着已撤权文档的页面与引用。 本次新增第 5 条通道:库级 wiki 快照,挂在现有文档发布包里,检索与 wiki 同一个 release 单元(同一次执行、同一次校验、同一次回滚): - 导出:ExportWiki 按 knowledge_base_id 全量读 wiki_pages/wiki_folders; 收敛门禁(库内无解析中/合成中文档)不满足直接失败并给中文原因; 未启用 wiki 且无 wiki 行的库不参与同步(生产 wiki 保持原样)。 - 撤权:item.Action == "delete" 时在导出端剥离该文档的 wiki 贡献 (source_refs/chunk_refs 去条目、无引用页面整页不带、摘死链、清 parent_slug、 无页面引用的空文件夹及其祖先一并去掉),使“源端仍在只停止发布”和 “源端删除”两种情况收敛到同一结果。 - 导入:库级 replace-by-kb(同一事务内 DELETE by kb 后灌入快照,文件夹先于页面), 新增 ReplaceWiki 供撤权路径使用(文档行只 disable,wiki 整体替换)。 - 校验:VerifyWiki 用生产 WeKnora wiki API 比对页面 slug 集合,撤权时额外断言 生产 wiki 不再引用被撤权文档。 驱动与验收:新增 cmd/release-channel-e2e(复用发布 worker 真实代码路径, 支持 publish/withdraw/rollback),scripts/wiki_e2e_test5.sh、 scripts/wiki_e2e_newkb.sh 两套端到端脚本;证据见 docs/WIKI_CO_TRANSPORT_ACCEPTANCE.md。
208 lines
6.9 KiB
Go
208 lines
6.9 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)
|
|
ListWikiPages(ctx context.Context, knowledgeBaseID string, page, pageSize int) ([]weknora.WikiPageSummary, int64, 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])
|
|
}
|
|
|
|
// VerifyWiki 校验生产端 wiki 与本次快照一致:
|
|
// - 页面 slug 集合必须与快照完全相同(多一个少一个都算失败,这样“撤权后
|
|
// 生产还留着已撤权文档的页面”会被抓住);
|
|
// - 撤权(delete)时生产 wiki 任何页面都不得再引用被撤权的文档。
|
|
//
|
|
// 老包 / 未启用 wiki 的库不携带 wiki 快照,直接跳过(生产 wiki 保持原样)。
|
|
func (verifier *WeKnoraVerifier) VerifyWiki(ctx context.Context, item releaseworkflow.PlanItem, bundle *DocumentBundle) error {
|
|
if bundle == nil || validateDocumentBundle(bundle) != nil {
|
|
return errors.New("invalid WeKnora wiki verification bundle")
|
|
}
|
|
if !hasWikiSnapshot(bundle.Postgres) {
|
|
return nil
|
|
}
|
|
expected := map[string]bool{}
|
|
for _, raw := range bundle.Postgres.Table("wiki_pages").Rows {
|
|
var row struct {
|
|
Slug string `json:"slug"`
|
|
}
|
|
if json.Unmarshal(raw, &row) != nil || strings.TrimSpace(row.Slug) == "" {
|
|
return errors.New("invalid WeKnora wiki verification bundle")
|
|
}
|
|
expected[row.Slug] = true
|
|
}
|
|
const pageSize = 20
|
|
const maxPageBudget = 1000
|
|
seen := map[string]bool{}
|
|
scanned := 0
|
|
for page := 1; ; page++ {
|
|
pages, total, err := verifier.client.ListWikiPages(ctx, item.TestKnowledgeBaseID, page, pageSize)
|
|
if err != nil {
|
|
return errors.New("production WeKnora wiki verification failed")
|
|
}
|
|
for _, wikiPage := range pages {
|
|
if strings.TrimSpace(wikiPage.Slug) == "" {
|
|
continue
|
|
}
|
|
seen[wikiPage.Slug] = true
|
|
if item.Action != "delete" {
|
|
continue
|
|
}
|
|
for _, reference := range wikiPage.SourceRefs {
|
|
if referencesKnowledge(reference, item.KnowledgeID) {
|
|
return errors.New("production WeKnora wiki still references the withdrawn document")
|
|
}
|
|
}
|
|
}
|
|
scanned += len(pages)
|
|
if len(pages) == 0 || int64(scanned) >= total {
|
|
break
|
|
}
|
|
if page >= maxPageBudget {
|
|
return errors.New("production WeKnora wiki verification exceeded its page budget")
|
|
}
|
|
}
|
|
if len(seen) != len(expected) {
|
|
return errors.New("production WeKnora wiki page set mismatch")
|
|
}
|
|
for slug := range expected {
|
|
if !seen[slug] {
|
|
return errors.New("production WeKnora wiki page set mismatch")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
var _ ProductionVerifier = (*WeKnoraVerifier)(nil)
|