feat(release): 发布目标知识库可选(源库 → 目标库映射)
- 迁移 000044:production_kb_bindings + release_item_executions.target_knowledge_base_id - 系统设置新增「生产知识库绑定(对内/对外)」:从生产 WeKnora 实际列表选库, 保存时真校验(不存在拒收;生产端不可达只提示并如实回报),仅管理员可改 - 执行计划按绑定解析目标库并冻结进 release_item_executions;空目标直接拒收; 已发布过的条目保留原目标(重试/回滚回到同一处) - DocumentBundle.RemapKnowledgeBase:knowledges/chunks/knowledge_tags/wiki_pages/ wiki_folders 的 knowledge_base_id 与 ES 文档字段改写为目标库,重算表/文档摘要并重新封装 - 契约比对改为「声明的映射对」(KnowledgeBaseMapping),其余字段继续 fail-closed - 契约检查器 / 目标身份 / 验证器全部走映射后的目标库;对象通道无需改写 - Compose 拆分:数据面接线 compose.release-sim.yaml + 两道门禁 compose.release-gates.yaml - 测试:RemapKnowledgeBase 全通道/无操作/fail-closed、映射契约、绑定接口、RBAC、真实 PG 集成
This commit is contained in:
@@ -99,7 +99,27 @@ func Seal(manifest Manifest) (Manifest, error) {
|
||||
return manifest, nil
|
||||
}
|
||||
|
||||
func Validate(manifest Manifest, target TargetContract) error {
|
||||
// KnowledgeBaseMapping is the declared 源库 → 目标库 pair for a release item.
|
||||
// An empty TargetKnowledgeBaseID means the historical same-id semantics: the
|
||||
// production base must carry exactly the tested base's id.
|
||||
type KnowledgeBaseMapping struct {
|
||||
SourceKnowledgeBaseID string
|
||||
TargetKnowledgeBaseID string
|
||||
}
|
||||
|
||||
// EffectiveTarget is the base the release writes into.
|
||||
func (mapping KnowledgeBaseMapping) EffectiveTarget() string {
|
||||
if trimmed := strings.TrimSpace(mapping.TargetKnowledgeBaseID); trimmed != "" {
|
||||
return trimmed
|
||||
}
|
||||
return mapping.SourceKnowledgeBaseID
|
||||
}
|
||||
|
||||
// Validate proves the bundle contract against the target data plane. The only
|
||||
// thing a cross-base release relaxes is the base identity itself: the bundle
|
||||
// keeps the SOURCE base and the target must match the DECLARED mapping, so an
|
||||
// undeclared (or stale) mapping still fails closed.
|
||||
func Validate(manifest Manifest, target TargetContract, mapping KnowledgeBaseMapping) error {
|
||||
if err := validateManifestFields(manifest); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -114,9 +134,15 @@ func Validate(manifest Manifest, target TargetContract) error {
|
||||
return errors.New("bundle digest mismatch")
|
||||
}
|
||||
contract := manifest.Contract
|
||||
if mapping.SourceKnowledgeBaseID != contract.KnowledgeBaseID {
|
||||
return errors.New("release mapping does not match bundle")
|
||||
}
|
||||
if target.KnowledgeBaseID != mapping.EffectiveTarget() {
|
||||
return errors.New("target release contract mismatch")
|
||||
}
|
||||
if target.WeKnoraVersion != WeKnoraVersionV071 || target.MigrationVersion != WeKnoraMigrationV071 ||
|
||||
contract.WeKnoraVersion != target.WeKnoraVersion || contract.MigrationVersion != target.MigrationVersion ||
|
||||
contract.TenantID != target.TenantID || contract.KnowledgeBaseID != target.KnowledgeBaseID ||
|
||||
contract.TenantID != target.TenantID ||
|
||||
contract.RetrieverEngine != target.RetrieverEngine || contract.EmbeddingModel != target.EmbeddingModel ||
|
||||
contract.EmbeddingDimension != target.EmbeddingDimension ||
|
||||
contract.ElasticsearchMappingSHA256 != target.ElasticsearchMappingSHA256 {
|
||||
|
||||
@@ -40,7 +40,7 @@ func TestSealAndValidateV071Manifest(t *testing.T) {
|
||||
EmbeddingModel: "bge-m3", EmbeddingDimension: 1024,
|
||||
ElasticsearchMappingSHA256: strings.Repeat("a", 64),
|
||||
}
|
||||
if err := Validate(sealed, target); err != nil {
|
||||
if err := Validate(sealed, target, sameIDMapping()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
encoded, err := json.Marshal(sealed)
|
||||
@@ -96,7 +96,7 @@ func TestValidateRejectsContractDriftAndUnknownTables(t *testing.T) {
|
||||
candidate, contract := manifest, target
|
||||
candidate.Tables = append([]TableDigest(nil), manifest.Tables...)
|
||||
tt.mutate(&candidate, &contract)
|
||||
if err := Validate(candidate, contract); err == nil {
|
||||
if err := Validate(candidate, contract, sameIDMapping()); err == nil {
|
||||
t.Fatal("Validate() error=nil")
|
||||
}
|
||||
})
|
||||
@@ -117,3 +117,51 @@ func validManifest() Manifest {
|
||||
func validTarget() TargetContract {
|
||||
return TargetContract{WeKnoraVersion: "v0.7.1", MigrationVersion: 74, TenantID: 10000, KnowledgeBaseID: "kb-safe", RetrieverEngine: "elasticsearch_v8", EmbeddingModel: "bge-m3", EmbeddingDimension: 1024, ElasticsearchMappingSHA256: strings.Repeat("a", 64)}
|
||||
}
|
||||
|
||||
// sameIDMapping is the historical semantics: no declared target, so the
|
||||
// production base must carry the tested base's id.
|
||||
func sameIDMapping() KnowledgeBaseMapping {
|
||||
return KnowledgeBaseMapping{SourceKnowledgeBaseID: "kb-safe"}
|
||||
}
|
||||
|
||||
// A declared 源库 → 目标库 mapping lets the bundle land in a DIFFERENT base,
|
||||
// which is the whole point of the production binding setting.
|
||||
func TestValidateAcceptsDeclaredCrossBaseMapping(t *testing.T) {
|
||||
manifest, err := Seal(validManifest())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
target := validTarget()
|
||||
target.KnowledgeBaseID = "prod-kb"
|
||||
mapping := KnowledgeBaseMapping{SourceKnowledgeBaseID: "kb-safe", TargetKnowledgeBaseID: "prod-kb"}
|
||||
if err := Validate(manifest, target, mapping); err != nil {
|
||||
t.Fatalf("declared cross-base release must validate: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// An undeclared, stale or self-inconsistent mapping must fail closed: neither
|
||||
// "target drifted somewhere else" nor "mapping points at the source" passes.
|
||||
func TestValidateRejectsUndeclaredOrDriftedMapping(t *testing.T) {
|
||||
manifest, err := Seal(validManifest())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
mapping KnowledgeBaseMapping
|
||||
target string
|
||||
}{
|
||||
{name: "mapping source does not match bundle", mapping: KnowledgeBaseMapping{SourceKnowledgeBaseID: "other"}, target: "other"},
|
||||
{name: "target differs from declared mapping", mapping: KnowledgeBaseMapping{SourceKnowledgeBaseID: "kb-safe", TargetKnowledgeBaseID: "prod-kb"}, target: "another-kb"},
|
||||
{name: "mapping target differs while target keeps source id", mapping: KnowledgeBaseMapping{SourceKnowledgeBaseID: "kb-safe", TargetKnowledgeBaseID: "prod-kb"}, target: "kb-safe"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
target := validTarget()
|
||||
target.KnowledgeBaseID = tt.target
|
||||
if err := Validate(manifest, target, tt.mapping); err == nil {
|
||||
t.Fatal("Validate() error=nil")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user