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:
姚章浩
2026-10-08 15:41:13 +08:00
parent cddc4c90d1
commit 6440e3f679
40 changed files with 1823 additions and 92 deletions
+44
View File
@@ -9,6 +9,7 @@ import (
"testing"
"time"
postgresstore "knowledge-governance/internal/platform/postgres"
"knowledge-governance/internal/platform/weknora"
)
@@ -16,6 +17,11 @@ type bindingStoreFake struct {
internal string
external string
saver string
// replan records what the last save asked for, so tests can assert that the
// effective (env-fallback aware) targets were used.
replanEffective [2]string
replanResult postgresstore.RoutingBindingReplanResult
replanCallCount int
}
func (fake *bindingStoreFake) LoadRoutingKBBindings(context.Context) (string, string, string, error) {
@@ -27,6 +33,13 @@ func (fake *bindingStoreFake) SaveRoutingKBBindings(_ context.Context, internal,
return nil
}
func (fake *bindingStoreFake) SaveRoutingKBBindingsWithReplan(_ context.Context, internal, external, effectiveInternal, effectiveExternal, updatedBy string, _ time.Time) (postgresstore.RoutingBindingReplanResult, error) {
fake.internal, fake.external, fake.saver = internal, external, updatedBy
fake.replanEffective = [2]string{effectiveInternal, effectiveExternal}
fake.replanCallCount++
return fake.replanResult, nil
}
type kbListerFake struct {
bases []weknora.KnowledgeBase
err error
@@ -104,6 +117,37 @@ func TestRoutingBindingsSaveValidatesAgainstWeKnora(t *testing.T) {
}
}
// A binding change must also carry the 待入库 re-plan counts and pass the
// EFFECTIVE targets (env fallback included) so an unbound side still routes.
func TestRoutingBindingsSaveReportsReplanAndUsesEffectiveTargets(t *testing.T) {
store := &bindingStoreFake{replanResult: postgresstore.RoutingBindingReplanResult{Replanned: 3, RetiredDuplicates: 1}}
lister := &kbListerFake{bases: []weknora.KnowledgeBase{{ID: "kb-in", Name: "对内库"}, {ID: "kb-ex", Name: "对外库"}}}
handler := bindingsTestHandler(t, store, lister, "env-internal", "env-external")
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPut, "/api/v1/routing-bindings", strings.NewReader(`{"internal_kb_id":"kb-in","external_kb_id":""}`)))
if recorder.Code != http.StatusOK {
t.Fatalf("save must succeed: %d %s", recorder.Code, recorder.Body.String())
}
if store.replanCallCount != 1 {
t.Fatalf("save must go through the replanning path once, got %d", store.replanCallCount)
}
if store.replanEffective != [2]string{"kb-in", "env-external"} {
t.Fatalf("effective targets = %#v, want the env fallback on the unbound side", store.replanEffective)
}
var envelope struct {
Data struct {
Replanned int `json:"replanned"`
RetiredDuplicates int `json:"retired_duplicates"`
} `json:"data"`
}
if err := json.Unmarshal(recorder.Body.Bytes(), &envelope); err != nil {
t.Fatal(err)
}
if envelope.Data.Replanned != 3 || envelope.Data.RetiredDuplicates != 1 {
t.Fatalf("replan counts = %#v", envelope.Data)
}
}
func TestRouteCatalogFilesUsesUIBinding(t *testing.T) {
store := &bindingStoreFake{internal: "ui-internal", external: "ui-external"}
handler := bindingsTestHandler(t, store, &kbListerFake{}, "env-internal", "env-external")