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:
@@ -0,0 +1,150 @@
|
||||
package pp
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
)
|
||||
|
||||
type ImportPlan struct {
|
||||
Connection governance.SourceConnection
|
||||
SourceItems []governance.SourceItem
|
||||
ContentObjects []governance.ContentObject
|
||||
Versions []governance.SourceItemVersion
|
||||
Publications []governance.Publication
|
||||
}
|
||||
|
||||
func BuildImportPlan(snapshot Snapshot) (ImportPlan, error) {
|
||||
if err := ValidateSnapshot(snapshot); err != nil {
|
||||
return ImportPlan{}, err
|
||||
}
|
||||
connectionID := stableID("sc", snapshot.KBID)
|
||||
plan := ImportPlan{
|
||||
Connection: governance.SourceConnection{
|
||||
ID: connectionID,
|
||||
Type: governance.SourceTypePP,
|
||||
Name: "PP legacy " + connectionID,
|
||||
SecretRef: "",
|
||||
Enabled: false,
|
||||
},
|
||||
SourceItems: make([]governance.SourceItem, 0, len(snapshot.Items)),
|
||||
ContentObjects: make([]governance.ContentObject, 0),
|
||||
Versions: make([]governance.SourceItemVersion, 0, len(snapshot.Items)),
|
||||
Publications: make([]governance.Publication, 0),
|
||||
}
|
||||
contentBySHA := make(map[string]governance.ContentObject)
|
||||
publicationByKnowledgeID := make(map[string]struct{})
|
||||
for _, item := range snapshot.Items {
|
||||
itemID := stableID("si", connectionID, item.SourceURLHash)
|
||||
sizeBytes := item.Bytes
|
||||
revision := item.SHA256
|
||||
if item.Status == StatusOverLimit {
|
||||
revision = metadataRevision(item)
|
||||
}
|
||||
plan.SourceItems = append(plan.SourceItems, governance.SourceItem{
|
||||
ID: itemID,
|
||||
SourceConnectionID: connectionID,
|
||||
StableExternalID: item.SourceURLHash,
|
||||
CanonicalURIHash: item.SourceURLHash,
|
||||
Title: "",
|
||||
ItemType: governance.ItemTypeDocument,
|
||||
SizeBytes: &sizeBytes,
|
||||
Revision: revision,
|
||||
Visibility: governance.VisibilityExternal,
|
||||
VisibilityOrigin: governance.VisibilityOriginPPPublicTag,
|
||||
SourceChange: governance.SourceChangeUnchanged,
|
||||
})
|
||||
|
||||
rawSummary := map[string]any{
|
||||
"legacy_status": item.Status,
|
||||
"source_ids": item.SourceIDs,
|
||||
}
|
||||
if item.Status == StatusContentAlias {
|
||||
rawSummary["canonical_source_url_hash"] = item.CanonicalSourceURLHash
|
||||
}
|
||||
rawJSON, err := json.Marshal(rawSummary)
|
||||
if err != nil {
|
||||
return ImportPlan{}, fmt.Errorf("marshal legacy record summary: %w", err)
|
||||
}
|
||||
versionID := stableID("sv", itemID, revision)
|
||||
if item.Status == StatusOverLimit {
|
||||
plan.Versions = append(plan.Versions, governance.SourceItemVersion{
|
||||
ID: versionID,
|
||||
SourceItemID: itemID,
|
||||
Revision: revision,
|
||||
ContentState: governance.ContentOverLimit,
|
||||
DownloadError: "legacy_50_mib_limit",
|
||||
RawRecordSummary: rawJSON,
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
content, ok := contentBySHA[item.SHA256]
|
||||
if !ok {
|
||||
content = governance.ContentObject{
|
||||
ID: stableID("co", item.SHA256),
|
||||
SHA256: item.SHA256,
|
||||
SizeBytes: item.Bytes,
|
||||
State: governance.ContentVerified,
|
||||
}
|
||||
contentBySHA[item.SHA256] = content
|
||||
plan.ContentObjects = append(plan.ContentObjects, content)
|
||||
} else if content.SizeBytes != item.Bytes {
|
||||
return ImportPlan{}, fmt.Errorf("same SHA-256 has conflicting byte sizes")
|
||||
}
|
||||
|
||||
contentID := content.ID
|
||||
plan.Versions = append(plan.Versions, governance.SourceItemVersion{
|
||||
ID: versionID,
|
||||
SourceItemID: itemID,
|
||||
Revision: item.SHA256,
|
||||
ContentObjectID: &contentID,
|
||||
ContentState: governance.ContentVerified,
|
||||
RawRecordSummary: rawJSON,
|
||||
})
|
||||
if item.Status != StatusCompleted {
|
||||
continue
|
||||
}
|
||||
if _, exists := publicationByKnowledgeID[item.KnowledgeID]; exists {
|
||||
return ImportPlan{}, fmt.Errorf("completed records share a knowledge ID")
|
||||
}
|
||||
publicationByKnowledgeID[item.KnowledgeID] = struct{}{}
|
||||
plan.Publications = append(plan.Publications, governance.Publication{
|
||||
ID: stableID("tp", snapshot.KBID, item.KnowledgeID),
|
||||
SourceItemVersionID: versionID,
|
||||
TestKBID: snapshot.KBID,
|
||||
KnowledgeID: item.KnowledgeID,
|
||||
IngestState: governance.IngestUploaded,
|
||||
ProcessingState: governance.ProcessingCompleted,
|
||||
RetrievalState: governance.RetrievalUnpublished,
|
||||
})
|
||||
}
|
||||
return plan, nil
|
||||
}
|
||||
|
||||
func stableID(prefix string, parts ...string) string {
|
||||
digest := sha256.New()
|
||||
for _, part := range parts {
|
||||
_, _ = digest.Write([]byte{0})
|
||||
_, _ = digest.Write([]byte(part))
|
||||
}
|
||||
return prefix + "-" + hex.EncodeToString(digest.Sum(nil))[:32]
|
||||
}
|
||||
|
||||
func metadataRevision(item Item) string {
|
||||
sourceIDs := append([]string(nil), item.SourceIDs...)
|
||||
sort.Strings(sourceIDs)
|
||||
parts := []string{item.SourceURLHash, strconv.FormatInt(item.Bytes, 10)}
|
||||
parts = append(parts, sourceIDs...)
|
||||
digest := sha256.New()
|
||||
for _, part := range parts {
|
||||
_, _ = digest.Write([]byte{0})
|
||||
_, _ = digest.Write([]byte(part))
|
||||
}
|
||||
return "metadata-sha256:" + hex.EncodeToString(digest.Sum(nil))
|
||||
}
|
||||
Reference in New Issue
Block a user