package main import ( "context" "fmt" "knowledge-governance/internal/governance" ) // libraryVersionIndex 是"当前生效版本"与"某个快照自己的登记"的精确查询口。 // // 为什么不是 LibraryVersions(scope, N):那是分页展示用的窗口,历史超过上限后 // "存在"会被读成"不存在"。接管/基线/同步闸口的判定都必须走这里,同一含义只有 // 一份实现(两个查询在 Store 里都是按条件精确命中,没有排序窗口)。 type libraryVersionIndex interface { LibraryCurrentVersion(ctx context.Context, scope string) (governance.LibraryVersion, bool, error) LibraryVersionOfSnapshot(ctx context.Context, scope string, kind governance.LibraryVersionKind, snapshotID string) (governance.LibraryVersion, bool, error) } // libraryVersionReader 是"当前生效版本 + 展示窗口"的组合:已知索引映射只需要这两样。 type libraryVersionReader interface { LibraryCurrentVersion(ctx context.Context, scope string) (governance.LibraryVersion, bool, error) LibraryVersions(ctx context.Context, scope string, limit int) ([]governance.LibraryVersion, error) } // libraryVersionFacts 是接管判定需要的两个事实: // - Current/HasCurrent:这个 scope 现在生效的是哪一份(每 scope 至多一行); // - Snapshot/HasSnapshot:本次作业的目标快照自己有没有生效登记(含"已顶替")。 // // Err 非空表示"读不到",调用方必须按未知处理,不许降级成"没有证据"。 type libraryVersionFacts struct { Current governance.LibraryVersion HasCurrent bool Snapshot governance.LibraryVersion HasSnapshot bool Err error } // loadLibraryVersionFacts 用两次精确查询拼出判定事实;任一查询失败都返回 Err。 func loadLibraryVersionFacts(ctx context.Context, store libraryVersionIndex, scope, targetSnapshotID string) libraryVersionFacts { facts := libraryVersionFacts{} current, hasCurrent, err := store.LibraryCurrentVersion(ctx, scope) if err != nil { facts.Err = fmt.Errorf("读不到当前生效版本:%w", err) return facts } facts.Current, facts.HasCurrent = current, hasCurrent if targetSnapshotID == "" { return facts } snapshot, hasSnapshot, err := store.LibraryVersionOfSnapshot(ctx, scope, governance.LibraryVersionProductionEffective, targetSnapshotID) if err != nil { facts.Err = fmt.Errorf("读不到快照 %s 的生效登记:%w", targetSnapshotID, err) return facts } facts.Snapshot, facts.HasSnapshot = snapshot, hasSnapshot return facts } // knownIndexMapping 取"上一次登记过的索引映射",作为装配目标时的映射基线。 // // 权威来源是**当前生效版本**的登记证据;只有在它没有记录映射时,才退回到最近 // 成功记录里的那一份(恢复会把旧版本顶替掉,这时仍要拿到生产实际用的映射)。 // 退化只会让"索引被改动"判定偏严,不会误放行覆盖。同步与恢复共用这一份实现。 func knownIndexMapping(ctx context.Context, store libraryVersionReader, scope string) string { if current, found, err := store.LibraryCurrentVersion(ctx, scope); err == nil && found { if raw, ok := current.Evidence["index_mapping_sha256"].(string); ok && raw != "" { return raw } } versions, err := store.LibraryVersions(ctx, scope, 50) if err != nil { return "" } for _, item := range versions { if raw, ok := item.Evidence["index_mapping_sha256"].(string); ok && raw != "" { return raw } } return "" }