From ecf518e3bbee81b3c53f77a6283d61c3afc360d0 Mon Sep 17 00:00:00 2001 From: dev Date: Tue, 17 Nov 2026 11:43:02 +0800 Subject: [PATCH] =?UTF-8?q?fix(library):=20=E5=9F=BA=E7=BA=BF/=E7=B4=A2?= =?UTF-8?q?=E5=BC=95=E6=8C=89=200.9.0=20profile=20=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=EF=BC=8C=E4=B8=8D=E5=86=8D=E5=86=99=E6=AD=BB=2074=20=E4=B8=8E?= =?UTF-8?q?=20tenant=5Fid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - OpenBaselineStack 用目标库解析出的 profile 校验 schema(134 合法), contract.WeKnoraVersion 取 profile 值 - no-gate 维护参数(IngressMode/IngressAck/HealthURL)从配置透传, 否则 0.9.0 目标会被当成缺闸门拒绝 - 索引写入与校验按 profile 决定是否补 tenant_id(0.9.0 索引无该字段) --- internal/libraryprofile/profile.go | 5 ++++ internal/librarystack/stack.go | 24 +++++++++++++++---- .../releaseadapter/v071/library_index_plan.go | 10 ++++---- .../v071/library_scope_probe.go | 2 +- .../v071/library_verify_content.go | 6 ++++- 5 files changed, 36 insertions(+), 11 deletions(-) diff --git a/internal/libraryprofile/profile.go b/internal/libraryprofile/profile.go index dfeb031..b95e2dd 100644 --- a/internal/libraryprofile/profile.go +++ b/internal/libraryprofile/profile.go @@ -44,6 +44,9 @@ type Profile struct { // IndexContractSource 说明该版本的索引名从哪里读。空值等于"读不到", // 不允许回退成空串去匹配任何索引。 IndexContractSource string + // IndexTenantField 表示该版本的索引文档带 tenant_id。0.9.0 的分块文档只有 + // knowledge_base_id,写索引时补 tenant_id 会与目标实际文档不一致。 + IndexTenantField bool } var profiles = map[int]Profile{ @@ -78,6 +81,7 @@ var profiles = map[int]Profile{ {"index", "chunks"}, }, IndexContractSource: "vector_stores.index_config.index_name", + IndexTenantField: true, }, V090Version: { Name: "v090", @@ -122,6 +126,7 @@ var profiles = map[int]Profile{ `CREATE TRIGGER trg_chunk_images_update AFTER UPDATE ON public.chunks FOR EACH ROW WHEN ((((old.image_info ~~ '[%'::text) OR (new.image_info ~~ '[%'::text)) AND ((old.image_info IS DISTINCT FROM new.image_info) OR (old.is_enabled IS DISTINCT FROM new.is_enabled) OR (old.status IS DISTINCT FROM new.status) OR (old.deleted_at IS DISTINCT FROM new.deleted_at) OR (old.tenant_id IS DISTINCT FROM new.tenant_id) OR ((old.knowledge_base_id)::text IS DISTINCT FROM (new.knowledge_base_id)::text) OR ((old.knowledge_id)::text IS DISTINCT FROM (new.knowledge_id)::text) OR ((old.chunk_type)::text IS DISTINCT FROM (new.chunk_type)::text) OR (old.created_at IS DISTINCT FROM new.created_at) OR (old.updated_at IS DISTINCT FROM new.updated_at)))) EXECUTE FUNCTION chunk_images_sync()`, }, IndexContractSource: "config ELASTICSEARCH_INDEX", + IndexTenantField: false, }, } diff --git a/internal/librarystack/stack.go b/internal/librarystack/stack.go index c5a0343..0cf2666 100644 --- a/internal/librarystack/stack.go +++ b/internal/librarystack/stack.go @@ -19,7 +19,6 @@ import ( "knowledge-governance/internal/librarysnapshot" "knowledge-governance/internal/objectgateway" v071 "knowledge-governance/internal/releaseadapter/v071" - "knowledge-governance/internal/releasebundle" ) // OpenMaintenance 按部署配置装配实例级维护目标;未接线时返回明确原因。 @@ -34,6 +33,9 @@ func OpenMaintenance(config appconfig.Config) (*librarymaintenance.Manager, erro Writers: config.LibraryTargetWriters, NonWriters: config.LibraryTargetNonWriters, DockerSocket: config.LibraryTargetDockerSocket, + IngressMode: config.LibraryTargetIngressMode, + IngressAck: config.LibraryTargetNoIngressAck, + HealthURL: config.LibraryTargetHealthURL, IngressURL: config.LibraryTargetIngressURL, IngressService: config.LibraryTargetIngressService, IngressDrainFile: config.LibraryTargetIngressDrainFile, @@ -298,10 +300,22 @@ func OpenBaselineStack(ctx context.Context, config appconfig.Config, scope strin target.Close() return nil, nil, fmt.Errorf("读取目标 WeKnora schema 版本失败: %w", err) } - if dirty || schemaVersion != releasebundle.WeKnoraMigrationV071 { + if dirty { target.Close() - return nil, nil, fmt.Errorf("目标 schema 版本 %d(dirty=%v)与受支持的 %d 不一致,拒绝建基线", - schemaVersion, dirty, releasebundle.WeKnoraMigrationV071) + return nil, nil, fmt.Errorf("目标 schema 版本 %d 处于 dirty 状态,拒绝建基线", schemaVersion) + } + // 契约按目标库自己的 schema 解析,而不是写死 74:0.9.0 的 schema 是 134, + // 它的表集合、索引来源与 Wiki 部件都和 0.7.1 不同,用 74 的常量建基线 + // 会把另一套栈的记录写进权威版本。 + profile, err := v071.ResolveSchemaProfile(ctx, target.Pool(), "baseline contract") + if err != nil { + target.Close() + return nil, nil, fmt.Errorf("解析目标 schema profile 失败: %w", err) + } + if profile.SchemaVersion != schemaVersion { + target.Close() + return nil, nil, fmt.Errorf("目标 schema 版本 %d 与解析出的 profile %s(%d)不一致,拒绝建基线", + schemaVersion, profile.Name, profile.SchemaVersion) } embeddingModel := config.LibraryEmbeddingModel embeddingDimension := config.LibraryEmbeddingDimension @@ -333,7 +347,7 @@ WHERE kb.id = $1 AND kb.deleted_at IS NULL`, scopeCfg.target.KnowledgeBaseID).Sc return nil, nil, fmt.Errorf("读取目标 embedding 模型绑定失败: %w", err) } contract := librarysnapshot.Compatibility{ - WeKnoraVersion: releasebundle.WeKnoraVersionV071, + WeKnoraVersion: profile.WeKnoraVersion, SchemaVersion: schemaVersion, RetrieverEngine: "elasticsearch_v8", EmbeddingModel: embeddingModel, diff --git a/internal/releaseadapter/v071/library_index_plan.go b/internal/releaseadapter/v071/library_index_plan.go index f81a491..8b4c390 100644 --- a/internal/releaseadapter/v071/library_index_plan.go +++ b/internal/releaseadapter/v071/library_index_plan.go @@ -43,7 +43,7 @@ func archiveIndexMap(archive *librarysnapshot.Archive, manifest librarysnapshot. } return out, nil } -func targetIndexSource(raw json.RawMessage, tenant uint64, kb string) (json.RawMessage, error) { +func targetIndexSource(raw json.RawMessage, tenant uint64, kb string, withTenant bool) (json.RawMessage, error) { var source map[string]json.RawMessage if json.Unmarshal(raw, &source) != nil || source == nil { return nil, errors.New("index source must be object") @@ -52,7 +52,9 @@ func targetIndexSource(raw json.RawMessage, tenant uint64, kb string) (json.RawM if json.Unmarshal(source["content"], &content) != nil || strings.HasPrefix(content, indexClaimPrefix) { return nil, errors.New("index source lacks string content or contains an unfinished transient claim") } - source["tenant_id"], _ = json.Marshal(tenant) + if withTenant { + source["tenant_id"], _ = json.Marshal(tenant) + } source["knowledge_base_id"], _ = json.Marshal(kb) encoded, err := json.Marshal(source) if err != nil { @@ -111,7 +113,7 @@ func (t *ProductionLibraryTarget) planIndex(ctx context.Context, archive *librar } var wanted json.RawMessage if raw, ok := after[id]; ok { - wanted, err = targetIndexSource(raw, t.scope.TenantID, t.scope.KnowledgeBaseID) + wanted, err = targetIndexSource(raw, t.scope.TenantID, t.scope.KnowledgeBaseID, t.schema.IndexTenantField) if err != nil { return err } @@ -151,7 +153,7 @@ func (t *ProductionLibraryTarget) validateIndexPlanBinding(archive *librarysnaps delete(ids, change.ID) var wanted json.RawMessage if raw, ok := after[change.ID]; ok { - wanted, err = targetIndexSource(raw, set.TenantID, set.KnowledgeBaseID) + wanted, err = targetIndexSource(raw, set.TenantID, set.KnowledgeBaseID, t.schema.IndexTenantField) if err != nil { return err } diff --git a/internal/releaseadapter/v071/library_scope_probe.go b/internal/releaseadapter/v071/library_scope_probe.go index 21872dc..ce11d83 100644 --- a/internal/releaseadapter/v071/library_scope_probe.go +++ b/internal/releaseadapter/v071/library_scope_probe.go @@ -24,7 +24,7 @@ func ScopeContentCounts(ctx context.Context, pool *pgxpool.Pool, identity Produc counts := make(map[string]int64, len(profile.Tables)) for _, table := range profile.Tables { var count int64 - if err := pool.QueryRow(ctx, fmt.Sprintf(`SELECT count(*) FROM %s WHERE %s`, table, scopeWhere(table)), + if err := pool.QueryRow(ctx, fmt.Sprintf(`SELECT count(*) FROM %s WHERE %s`, table, scopeWhereFor(profile.Tables, table)), identity.TenantID, identity.KnowledgeBaseID).Scan(&count); err != nil { return nil, fmt.Errorf("count %s: %w", table, err) } diff --git a/internal/releaseadapter/v071/library_verify_content.go b/internal/releaseadapter/v071/library_verify_content.go index c210554..3e7668e 100644 --- a/internal/releaseadapter/v071/library_verify_content.go +++ b/internal/releaseadapter/v071/library_verify_content.go @@ -276,7 +276,11 @@ func (t *ProductionLibraryTarget) verifyIndexDigest(ctx context.Context, archive if err := json.Unmarshal(document.Source, &source); err != nil { return errors.New("归档索引文档不是对象") } - source["tenant_id"], _ = json.Marshal(t.scope.TenantID) + // 0.9.0 的分块文档只有 knowledge_base_id(索引里没有 tenant_id), + // 规范化必须按目标 profile 来,否则"写对了"也会被判成生产缺少字段。 + if t.schema.IndexTenantField { + source["tenant_id"], _ = json.Marshal(t.scope.TenantID) + } source["knowledge_base_id"], _ = json.Marshal(t.scope.KnowledgeBaseID) encoded, err := json.Marshal(source) if err != nil {