fix(library): 当前生效版本改按精确查询

接管分类、基线判定、已知索引映射、同步闸口的创建判据与归档保留的保护标记,
原来都用 LibraryVersions(scope, 50/100) 这个分页窗口再在内存里筛。生效版本每
scope 只有一行、可能很久以前登记,历史一长就被挤出窗口 —— 于是"已生效"被读成
"没有生效":接管会把已经上线的目标重写一遍,基线入口会给已有权威版本的目标
再建一次基线。

现在统一走两条精确查询:LibraryCurrentVersion(kind='production_effective' AND
superseded_at IS NULL,每 scope 至多一行)与 LibraryVersionOfSnapshot(按快照
身份取回"已顶替"证据)。多行、自相矛盾、读不到一律 fail closed;同一含义只留
一份实现(knownIndexMapping 同步与恢复共用)。LibraryVersions 仍是 1..100 的
展示窗口,只用于列表。

验证:真实 PG 上先复现(窗口实现 ⇒ found=false),改后 7 条 store 契约用例与
worker 端到端判定用例全绿;本机 make verify 15 项 success(用例 1264 通过 /
0 失败 / 93 已声明可选跳过)。
This commit is contained in:
姚章浩
2026-10-30 03:09:21 +08:00
parent 67743f2285
commit 0088004cd6
15 changed files with 655 additions and 130 deletions
+29 -7
View File
@@ -342,6 +342,11 @@ type libraryJobStore interface {
ListLibraryJobs(ctx context.Context, scope string, limit int) ([]governance.LibraryJob, error)
RecordLibraryVersion(ctx context.Context, version governance.LibraryVersion) error
LibraryVersions(ctx context.Context, scope string, limit int) ([]governance.LibraryVersion, error)
// 当前生效版本与"某快照自己的登记"必须是精确查询:LibraryVersions 是分页展示窗口,
// 窗口一满就会把"已生效"读成"没有",同步闸口/基线入口就会放行一次不必要的重写。
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)
UpdateLibraryStage(ctx context.Context, jobID, stage, state string, total, completed, bytesTotal, bytesDone *int64, detail string, now time.Time) error
AdvanceLibraryJob(ctx context.Context, jobID string, state governance.LibraryJobState, stage, message, recovery, snapshotID, targetSnapshotID string, now time.Time) error
AttachLibraryQueueJob(ctx context.Context, jobID, queueJobID string, now time.Time) error
@@ -678,16 +683,27 @@ func libraryOverviewHandler(w http.ResponseWriter, r *http.Request, config Confi
}
// 生产生效 / 预备份 / 作业来自持久登记。
// 历史列表仍是分页展示(窗口),但"当前生效版本"必须精确查:窗口一满,
// 生效版本会掉出列表,页面就会显示成"没有生效版本",与真实状态不符。
versions, err := store.LibraryVersions(r.Context(), scope, 50)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot read library versions"})
return
}
current, hasCurrent, currentErr := store.LibraryCurrentVersion(r.Context(), scope)
if currentErr != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot read current library version"})
return
}
if archiveReadable {
overview.TestSealed, overview.TestSealedReason = testSealedOf(versions, byID)
}
overview.Production = libraryKnownView{State: "unknown", Reason: "还没有生产验证通过的生效版本记录"}
overview.Prebackup = libraryKnownView{State: "unknown", Reason: "这个范围还没有任何生产预备份"}
if hasCurrent {
view := libraryVersionViewOf(current)
overview.Production = libraryKnownView{State: "known", Version: &view}
}
target := ""
if overview.TestSealed != nil {
target = overview.TestSealed.SnapshotID
@@ -701,9 +717,7 @@ func libraryOverviewHandler(w http.ResponseWriter, r *http.Request, config Confi
})
switch item.Kind {
case governance.LibraryVersionProductionEffective:
if item.SupersededAt == nil {
overview.Production = libraryKnownView{State: "known", Version: &view}
}
// 生效状态只认上面那条精确查询;这里只负责把历史行放进列表。
case governance.LibraryVersionProductionPrebackup:
// 页面要显示"最近一次生产写入前的备份",不是"与拟发布快照同内容的那一份":
// 恢复作业登记的是**恢复前**的备份,它和测试封存快照本来就不同;
@@ -833,12 +847,24 @@ func librarySyncEnqueue(w http.ResponseWriter, r *http.Request, config Config) {
return
}
overview := libraryOverview{Scope: request.Scope}
// 闸口依赖"生产是否已有生效版本":这是**创建**判据,必须精确查。
// 用窗口列表会把它读成"没有",于是把一次不必要的覆盖当成合法首发;
// 查询失败同样拒绝(fail closed),不猜。
current, hasCurrent, currentErr := store.LibraryCurrentVersion(r.Context(), request.Scope)
if currentErr != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot read current library version"})
return
}
versions, err := store.LibraryVersions(r.Context(), request.Scope, 50)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot read library versions"})
return
}
overview.Production = libraryKnownView{State: "unknown"}
if hasCurrent {
view := libraryVersionViewOf(current)
overview.Production = libraryKnownView{State: "known", Version: &view}
}
overview.Prebackup = libraryKnownView{State: "unknown"}
if empty, probeErr := productionScopeEmpty(r.Context(), config, request.Scope); probeErr == nil && empty {
overview.Production = libraryKnownView{State: "unknown", EmptyBaseline: true,
@@ -846,10 +872,6 @@ func librarySyncEnqueue(w http.ResponseWriter, r *http.Request, config Config) {
}
for index := range versions {
item := versions[index]
if item.Kind == governance.LibraryVersionProductionEffective && item.SupersededAt == nil {
view := libraryVersionViewOf(item)
overview.Production = libraryKnownView{State: "known", Version: &view}
}
if item.Kind == governance.LibraryVersionProductionPrebackup && request.Target != "" && item.SnapshotID == request.Target {
view := libraryVersionViewOf(item)
overview.Prebackup = libraryKnownView{State: "known", Version: &view}